I have a program in Go that I want to compile in a bunch of binaries, each having a const
value defined differently. More clearly, I have something like that:
const wordLen = 6
type knowledge [wordLen]byte
Here, wordLen
is associated with the value 6, but I want to have different binaries, with values ranging from 5 to 10. I could make it a variable, and then use a slice rather than an array, but that would have a huge performance impact on my soft (yes, I tried).
I would love to have some build tag on go build
argument to indicate what the value of wordLen
is for a given binary. So, what is the (as idiomatic as possible) way to do this ?
Yes, this is possible using Build Constraints.
You can supply a list of these constraints to go build
using the -tags
flag.
Example:
package main
import "fmt"
func main() {
fmt.Println(f)
}
// +build foo
package main
const f = "defined in foo.go"
// +build bar
package main
const f = "defined in bar.go"
Compiling the code with different tags will give different results:
$ go build -tags foo
$ ./main
defined in foo.go
$ go build -tags bar
$ ./main
defined in bar.go
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With