How do I set a pkg's version?

104 views
Skip to first unread message

Mark

unread,
Nov 1, 2022, 3:36:17 PM11/1/22
to golang-nuts
I am creating a pkg.
It has a `go.mod` file:
```

go 1.19
```
And code files with functions in .go files all in the mypkg pkg.

How do I specify which version the mypkg pkg is? I know about using `$ git tag vX.Y.Z` and `$ git push origin vX.Y.Z`, but is there any way I can have this version in a file that can be accessed at build time or runtime?

Chris Burkert

unread,
Nov 1, 2022, 8:55:25 PM11/1/22
to Mark, golang-nuts
During CI/CD we create a json file with a few details (git tag, branch, hash, date, time). Afterwards we compile Go Code which embeds this file into the binary. During runtime flags like --version print the json.

Note that this is about the version of some binary - not the version of a package. However, you could embed go.mod. But there may be better ways.

Hope this helps.

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/0a16b738-59e5-4885-90c8-cd168e308623n%40googlegroups.com.

Axel Wagner

unread,
Nov 1, 2022, 9:15:02 PM11/1/22
to golang-nuts
It feels like an oversight not to mention debug.BuildInfo here. No CI/CD or manual build steps required.

Mark

unread,
Nov 2, 2022, 8:31:29 AM11/2/22
to golang-nuts
I hadn't realised about debug.BuildInfo - thanks!

Mark

unread,
Nov 2, 2022, 8:47:50 AM11/2/22
to golang-nuts
Oh, except that debug.BuildInfo (unsurprisingly) is only available in debug not release builds. So I guess the answer is that there isn't any nice way to do it.

Jakob Borg

unread,
Nov 2, 2022, 8:55:00 AM11/2/22
to Mark, golang-nuts
On 2 Nov 2022, at 09:47, 'Mark' via golang-nuts <golan...@googlegroups.com> wrote:

Oh, except that debug.BuildInfo (unsurprisingly) is only available in debug not release builds. So I guess the answer is that there isn't any nice way to do it.

What do you mean by debug vs release builds? I don't think Go makes that distinction.

That said, the build info will not contain information about your tags and such. The traditional way to add that is to have a build script inject that using `-ldflags -X ...` to set the value of some variables in the program.

//jb

Mark

unread,
Nov 2, 2022, 9:05:28 AM11/2/22
to golang-nuts
If I add an import for debug and this code:
```
    if info, ok := debug.ReadBuildInfo(); ok {
        fmt.Println("###", info.Main.Version)
    }
```
My lib fails to build with this error:
```
package github.com/me/mylib
    imports debug: no Go files in /home/me/opt/go/src/debug
```
(~/opt/go is where I have the go binary unpacked)

Mark

unread,
Nov 2, 2022, 9:18:45 AM11/2/22
to golang-nuts
I just realised my mistake: the correct import is "runtime/debug"; I just used "debug".
However,
```
    version := ""

    if info, ok := debug.ReadBuildInfo(); ok {
        version = info.Main.Version
    }
```
leaves version as ""

Chris Burkert

unread,
Nov 2, 2022, 10:49:21 AM11/2/22
to Jakob Borg, Mark, golang-nuts
We used the traditional way for a long time. But with the embed package we stopped this. This is a good blog post on the topic: https://levelup.gitconnected.com/a-better-way-than-ldflags-to-add-a-build-version-to-your-go-binaries-2258ce419d2d.

--
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.

Mark

unread,
Nov 2, 2022, 11:37:23 AM11/2/22
to golang-nuts
Thank you, that looks like a practical solution.

Axel Wagner

unread,
Nov 2, 2022, 12:55:22 PM11/2/22
to Jakob Borg, Mark, golang-nuts


On Wed, Nov 2, 2022, 09:54 Jakob Borg <ja...@kastelo.net> wrote:
On 2 Nov 2022, at 09:47, 'Mark' via golang-nuts <golan...@googlegroups.com> wrote:

Oh, except that debug.BuildInfo (unsurprisingly) is only available in debug not release builds. So I guess the answer is that there isn't any nice way to do it.

What do you mean by debug vs release builds? I don't think Go makes that distinction.

That said, the build info will not contain information about your tags and such.

BuildInfo.Settings should contain an entry with Key=-tags which contains the tags passed ob the command line. At least with Go 1.19.

I think debug.BuildInfo really is what you want, unless your needs are fairly exotic. As long as your build process reduces to an invocation of the go tool, it should contain all the information you need. And reducing your build to an invocation of the go tool is almost always a good thing - at least in the open Go ecosystem.


The traditional way to add that is to have a build script inject that using `-ldflags -X ...` to set the value of some variables in the program.

//jb

--
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