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

Comments

Projects
None yet
3 participants
@lixingwang
Copy link

commented Oct 26, 2018

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

This comment has been minimized.

Copy link
Member

commented Oct 26, 2018

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 this to the Unplanned milestone Oct 26, 2018

@lixingwang

This comment has been minimized.

Copy link
Author

commented Oct 26, 2018

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

@myitcv

This comment has been minimized.

Copy link
Member

commented Oct 26, 2018

Closing per last comment.

@myitcv myitcv closed this Oct 26, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.