Please see the code of main.go and main_test.go pasted at the end. Then
consider the output of these commands. I would have expected the `go test`
command's output to contain the value 'hello'. From `go help test` output,
the `go test` command is supposed to honor all build flags, but clearly it
isn't.
$ go version
go version go1.7.1 darwin/amd64
$ pwd
/tmp/ldflags_test
$ go run -ldflags="-X main.MyVar=hello" main.go
MyVar's value is hello
$ go build -ldflags="-X main.MyVar=hello" && ./ldflags_test
MyVar's value is hello
$ go test -ldflags="-X main.MyVar=hello"
MyVar is empty
exit status 1
FAIL _/tmp/ldflags_test 0.016s
$ cat main.go
package main
import (
"fmt"
"os"
)
var MyVar = ""
func main() {
if MyVar == "" {
fmt.Println("MyVar is empty")
os.Exit(1)
} else {
fmt.Println("MyVar's value is " + MyVar)
}
}
$ cat main_test.go
package main
import (
"testing"
)
func TestMain(t *testing.T) {
main()
}
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.