简体   繁体   中英

Can I get correct version and build info when doing "go get path/to/myapp"?

I need to provide a version number in the output generated by Go app. For releases and local builds I use Makefile that has:

...
VERSION = $(shell git describe --tags)
VER = $(shell git describe --tags --abbrev=0)
DATE = $(shell date -u '+%Y-%m-%d_%H:%M:%S%Z')
...
FLAGS_LD=-ldflags "-X path/to/myapp/mypkg.Build=${DATE} \
                  -X path/to/myapp/mypkg.Version=${VERSION}"
...
GOCMD = go
GOBUILD = $(GOCMD) build $(FLAGS_LD)
GOINSTALL = $(GOCMD) install $(FLAGS_LD)
...

And in the package I have variables as

package mypkg

var (
    Version = "v0.11.0-dev"
    Build   string
)

All is well when I run build, install or release via make, but if I run go get path/to/myapp then -ldflags are empty and user gets outdated version information, and no build information.

Is there a way to get correct ldflags data when installing a program with go get ?

Is there a way to get correct ldflags data when installing a program with go get?

No.

No, you cannot read/write ldflags when downloading sources

go get is meant to install from sources.

Is there a way to get correct ldflags data when installing a program with go get?

The current response is no.

Yes, you can provide your users with your package's version

The simplest way to achieve what you're wanting to do is either :

  1. If what you want to share is a lib (re-useable code), add git tags to your source repo so that your dev-users can use it for package version management. (glide, go.mod, dep, ...).

  2. If what you want to share is a program (meant to be compiled and executed only), modify the value of Version each time you make a new release. You can perform this automatically very easily with a combination of git-hooks and sed .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM