dimus
dimus

Reputation: 9390

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?

Upvotes: 2

Views: 222

Answers (2)

snwfdhmp
snwfdhmp

Reputation: 401

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.

Upvotes: 2

Volker
Volker

Reputation: 42468

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

No.

Upvotes: 1

Related Questions