I am new to Go and as a former C++ programmer am a little confused on how to do this in Go. I want to add a compile time flag based on which I may include some test code.
I tried using -ldflags
, but am unable to go beyond a single variable.
This is what I tried: -ldflags "X main.var1 var1_value" "X main.var2 var2_value"
.
Am I doing the right thing here?
Consider using build constraints instead of the -X
flag. This, allows you to compile any code conditionally by doing something like
go install -tags 'foo bar'
If you still want to use the -X
flag multiple times, do it like this:
go install -ldflags '-X main.Foo 1 -X main.Bar 2' main.go
or (added and recommended in Go 1.5+)
go install -ldflags '-X main.Foo=1 -X main.Bar=2' main.go