Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/link: -X not applied if target is the result of a function call #29897

Closed
daenney opened this issue Jan 23, 2019 · 4 comments
Closed

cmd/link: -X not applied if target is the result of a function call #29897

daenney opened this issue Jan 23, 2019 · 4 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@daenney
Copy link

daenney commented Jan 23, 2019

What version of Go are you using (go version)?

$ go version
1.11.4

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/daenney/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/daenney/Development/go"
GOPROXY=""
GORACE=""
GOROOT="/home/daenney/.asdf/installs/golang/1.11.4/go"
GOTMPDIR=""
GOTOOLDIR="/home/daenney/.asdf/installs/golang/1.11.4/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build378681064=/tmp/go-build -gno-record-gcc-switches"

What did you do?

package main

import (
        "fmt"
        "time"
)

var (
        Version string = "1"
        Time string = time.Unix(0, 0).String()
)

func main() {
        fmt.Println(Version)
        fmt.Println(Time)
}
go build -o main -ldflags "-X main.Version=2 -X main.Time=3" main.go
./main

What did you expect to see?

2
3

What did you see instead?

2
1970-01-01 01:00:00 +0100 CET
@daenney
Copy link
Author

daenney commented Jan 23, 2019

I'm not sure if this is a bug or a lack of documentation but I couldn't find anything in go help build that would explain this.

If I change Time to be Time string = "0" the -ldflags applies as expected and I end up with 2 3 being printed.

@FiloSottile FiloSottile added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jan 23, 2019
@FiloSottile FiloSottile added this to the Go1.13 milestone Jan 23, 2019
@FiloSottile FiloSottile changed the title cmd/build: -X not applied if target is the result of a function call cmd/link: -X not applied if target is the result of a function call Jan 23, 2019
@FiloSottile
Copy link
Contributor

/cc @ianlancetaylor

@ianlancetaylor
Copy link
Contributor

The docs (https://golang.org/cmd/link) say "This is only effective if the variable is declared in the source code either uninitialized or initialized to a constant string expression. -X will not work if the initializer makes a function call or refers to other variables." So while this is unfortunate it's a known and documented limitation.

@daenney
Copy link
Author

daenney commented Jan 24, 2019

Ah, it had not occurred to me to check cmd/link instead. Thanks for the pointer!

@golang golang locked and limited conversation to collaborators Jan 24, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants