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/go: characters are missed after $ when using command: go build -ldflags "-X main.VarName=go$name" #28426

Closed
lixingwang opened this issue Oct 26, 2018 · 3 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Milestone

Comments

@lixingwang
Copy link

Please answer these questions before submitting your issue. Thanks!

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

go version go1.11.1 darwin/amd64

Does this issue reproduce with the latest release?

Yes. the latest release of 1.11.1

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

GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/xli/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/xli/gopath"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
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 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/1x/7jrlz5kn3bx870sltc31q36h0000gr/T/go-build935195579=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

The code

package main

import "fmt"

var VarName string

func main() {
	fmt.Println("var name:", VarName)
}

I try to overwrite the var VarName at build time use below command
go build -ldflags "-X main.VarName=go$name" -x

But the characters behind $ are missed,

See output main output.
var name: go

What did you expect to see?

I expected all characters set to var. the output expected to be var name: go$name

What did you see instead?

var name: go

@andybons
Copy link
Member

It's attempting to substitute the environment variable name.

If you enclose the -ldflags argument in single quotes it will not attempt to substitute $name with the environment variable.

$ cat issue28426.go
package main
import "fmt"
var VarName string
func main() { fmt.Println("var name:", VarName) }
$ export FOO=_hello
$ go build -ldflags '-X main.VarName=go$FOO' issue28426.go && ./issue28426 
var name: go$FOO
$ go build -ldflags "-X main.VarName=go$FOO" issue28426.go && ./issue28426
var name: go_hello

Does that help you with your issue?

@andybons andybons changed the title characters are missed after $ when using command: go build -ldflags "-X main.VarName=go$name" cmd/go: characters are missed after $ when using command: go build -ldflags "-X main.VarName=go$name" Oct 26, 2018
@andybons andybons added WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Oct 26, 2018
@andybons andybons added this to the Unplanned milestone Oct 26, 2018
@lixingwang
Copy link
Author

@andybons It's a magic :) Thanks a lot, it resolved my issue.

@myitcv
Copy link
Member

myitcv commented Oct 26, 2018

Closing per last comment.

@myitcv myitcv closed this as completed Oct 26, 2018
@golang golang locked and limited conversation to collaborators Oct 26, 2019
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. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

4 participants