I have a go program test.go
package main
import "fmt"
var DEBUG_MODE bool = true    
func main() {
  fmt.Println(DEBUG_MODE)
}
I want to set the DEBUG_MODE variable on the compile time to false
I've tried:
go build -ldflags "-X main.DEBUG_MODE 0" test.go && ./test 
true                                                                                                                                                                                                                             
kyz@s497:18:49:32:/tmp$ go build -ldflags "-X main.DEBUG_MODE false" test.go && ./test 
true                                                                                                                                                                                                                             
kyz@s497:18:49:41:/tmp$ go build -ldflags "-X main.DEBUG_MODE 0x000000000000" test.go && ./test                                                                                                                                  
true                                                                               
It doesn't work, but it works when DEBUG_MODE is a string
You can only  set string variables with -X linker flag. From the docs:
-X importpath.name=value
    Set the value of the string variable in importpath named name to value.
    Note that before Go 1.5 this option took two separate arguments.
    Now it takes one argument split on the first = sign.
You can use a string instead:
var DebugMode = "true"
and then
go build -ldflags "-X main.DebugMode=false" test.go && ./test
                        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