How to plug git version when using "go get" to fetch a cli?

75 views
Skip to first unread message

changkun

unread,
Nov 25, 2020, 12:25:11 PM11/25/20
to golang-nuts
Hi golang-nuts,

As far as I know, there are two approaches to add extra information at build time:
1. using -ldflags="-X path/pkg.Var=${ENV_VAR}", in a Makefile
2. using `go generate ./...`, before `go build`

These approaches are good as it is if I build my binary and distribute it to other users.
However, none of these are possible if a user uses `go get github.com/user/pkg/cmd/x-cli`, because:
1. `go get` does not understand Makefile
2. `go generate` does not execute with `go build` automatically.

What should I do in order to plug the extra information (in my case, the git version) if my user uses "go get"?

Jan Mercl

unread,
Nov 25, 2020, 12:36:17 PM11/25/20
to changkun, golang-nuts
I don't think that can be done. Also IINM, in module mode `go get` no
longer uses git. It just downloads the zipped version of the
appropriate tag via http from the hosting service.

changkun

unread,
Nov 25, 2020, 1:14:45 PM11/25/20
to golang-nuts
That is really unfortunate. Is there any alternative solution so that I can burn at least the version information to the binary when a user uses `go get`?

seank...@gmail.com

unread,
Nov 25, 2020, 1:37:13 PM11/25/20
to golang-nuts
using `runtime/debug` this will return a tagged or pseudo version when retrieved through `go get` in module mode, in other cases it will return `(devel)`

i, _ := debug.ReadBuildInfo()
fmt.Println(i.Main.Version)

Ian Davis

unread,
Nov 25, 2020, 2:33:08 PM11/25/20
to golan...@googlegroups.com
The version is really a property of the code (and binary) not the repository. I think it's better to put the version in the code that is committed.

This is the approach taken by packages such as protobuf https://pkg.go.dev/google.golang.org/prot...@v1.25.0/internal/version
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages