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: couldn't uses ldflags "-X" on custom type name alias to string: cannot set with -X: not a var of type string #44985

Closed
zhuah opened this issue Mar 13, 2021 · 1 comment

Comments

@zhuah
Copy link

@zhuah zhuah commented Mar 13, 2021

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

$ go version
go version go1.16.2 darwin/amd64

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/zhuah/Library/Caches/go-build"
GOENV="/Users/zhuah/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/zhuah/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/zhuah/go"
GOPRIVATE=""
GOPROXY="https://goproxy.cn,direct"
GOROOT="/usr/local/Cellar/gotip/stable"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/gotip/stable/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.16.2"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/8n/mb3y_4bj1xv4q9sdmgjqdx_c0000gn/T/go-build1446698321=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I defines a variable with custom type name aliased to string

type ID string

var id ID

func (v ID) Prefix() string { return "xxxx" }
func (v ID) Suffix() string { return "xxxx" }

and go build with -ldflags="-X pkg.id=aaa.bbb".

What did you expect to see?

What did you see instead?

go linker reports error: cannot set with -X: not a var of type string.

the ID type may contains complex format, It would be great if the linker supports custom type name,
so i could define methods on it and get type safety.

currently, i must defines the methods as global functions or create a new variable to wrap the linker variable.

var linkID string
var id = ID(linkID)
@zhuah zhuah changed the title couldn't uses ldflags "-X" on custom type name alias to string: cannot set with -X: not a var of type string cmd/link: couldn't uses ldflags "-X" on custom type name alias to string: cannot set with -X: not a var of type string Mar 15, 2021
@cherrymui
Copy link
Contributor

@cherrymui cherrymui commented Mar 15, 2021

Yes, the linker's -X flag is only for the built-in string type. As you posted, there is a simple workaround. Closing as work as intended. Thanks.

@cherrymui cherrymui closed this Mar 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants