How to set package variable using -ldflags -X in Golang build

Quoting from doc of Command link:

-X importpath.name=value
    Set the value of the string variable in importpath named name to value.
    Note that before Go 1.5 this option took two separate arguments.
    Now it takes one argument split on the first = sign.

So it can be used for any package, not just for the main package. But you must specify the full import path, not just the package name.

E.g. if your config package is located at $GOPATH/src/my/package/config, then use the following command:

go build -ldflags "-X my/package/config.Version=1.0.0" -o $(MY_BIN) $(MY_SRC)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)