Reputation: 9390
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
?
Upvotes: 2
Views: 222
Reputation: 401
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.
The simplest way to achieve what you're wanting to do is either :
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, ...).
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
.
Upvotes: 2
Reputation: 42468
Is there a way to get correct ldflags data when installing a program with go get?
No.
Upvotes: 1