为多个库版本构建Go绑定?

I've encountered a small hiccup when trying to merge the GTK3 support in the go-gtk fork used by pond with the upstream go-gtk based on GTK2.

These cgo pkg-config lines that specify the GTK version must afaik appear in every file, which prevents bindings common to both GTK 2 and 3 from existing in a common file.

I suppose the "idiomatic" hack to build compile a file against multiple GTK versions is : Place the common code in a foo_common.go file, so that it never builds. Use a perl or bash + sed script to build foo2.go and foo3.go from foo_common.go by fixing the cgo pkg-config lines and adding go build directives for a gtk2 tag.

You can use build constrain aka tags to achieve that.

First you have to split the files that use gtk2 and gtk3, then assuming you want gtk3 to be the default (I'll frown if you make gtk2 the default):

gtk3.go:

// +build !gtk2

package main

import "fmt"

func main() {
    fmt.Println("gtk3")
}

gtk2.go:

// +build gtk2

package main

import "fmt"

func main() {
    fmt.Println("gtk2")
}

Then to test it:

$ go build; and ./constrains
gtk3
$ go build -tags gtk2; and ./constrains
gtk2