I have the file playground.go
:
package main
import (
"fmt"
)
var GitCommit string
func main() {
fmt.Printf("Hello world, version: %s\n", GitCommit)
}
why the behavior of the build ldflag -X
depends on specifying the source filename?:
# CASE 1
$ go build -ldflags "-X main.GitCommit=abc" && ./go_playground
Hello world, version:
$ go tool nm go_playground
52a900 D main.GitCommit
# CASE 2
$ go build -ldflags "-X main.GitCommit=abc" playground.go && ./playground
Hello world, version: abc
$ go tool nm playground
5242f0 D main.GitCommit
4c5678 R main.GitCommit.str
EDIT: it seems that the problem happens when case 1 is executed from a symlinked directory; when case 1 is executed from the original directory, the variable is passed. I'm not sure if this is expected, or it's a bug.