cmd/link: "-X main.version" when version has a value and is later used in a global struct literal doesn't work #34675
Comments
@ianlancetaylor explained in #26042 (comment) |
The issue is that for something like:
the compiler recognizes that
Then later when we link with
because the linker doesn't know it needs to modify B as well. I see at least a few options here:
|
Change https://golang.org/cl/198657 mentions this issue: |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Steps
main.go
,go build
and run it.go build -ldflags "-X main.version=foo"
If you change
var version = "dev"
tovar version string
, it works. It also works if you justfmt.Println("Version:", version)
without going through the struct.The documentation says:
Which doesn't suggest to me that this should somehow not work.
This will be a common scenario when using https://github.com/spf13/cobra which uses a global struct like that with a
Version
field, and is where I encountered this originally.main.go
What did you expect to see?
"Version: foo" when running
go build -ldflags "-X main.version=foo"
.What did you see instead?
"Version: dev" when running
go build -ldflags "-X main.version=foo"
.The text was updated successfully, but these errors were encountered: