231

Is it possible to increment a minor version number automatically each time a Go app is compiled?

I would like to set a version number inside my program, with an autoincrementing section:

$ myapp -version
MyApp version 0.5.132

Being 0.5 the version number I set, and 132 a value that increments automatically each time the binary is compiled.

Is this possible in Go?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Sebastián Grignoli
  • 32,444
  • 17
  • 71
  • 86

7 Answers7

378

The Go linker (go tool link) has an option to set the value of an uninitialised string variable:

-X importpath.name=value
  Set the value of the string variable in importpath named name to

value. Note that before Go 1.5 this option took two separate arguments. Now it takes one argument split on the first = sign.

As part of your build process, you could set a version string variable using this. You can pass this through the go tool using -ldflags. For example, given the following source file:

package main

import "fmt"

var xyz string

func main() {
    fmt.Println(xyz)
}

Then:

$ go run -ldflags "-X main.xyz=abc" main.go
abc

In order to set main.minversion to the build date and time when building:

go build -ldflags "-X main.minversion=`date -u +.%Y%m%d.%H%M%S`" service.go

If you compile without initializing main.minversion in this way, it will contain the empty string.

wasmup
  • 14,541
  • 6
  • 42
  • 58
axw
  • 6,908
  • 1
  • 24
  • 14
  • 4
    Will that value be saved into the binary if I use `go bouild` instead of `go run`? – Sebastián Grignoli Jul 06 '12 at 04:17
  • 6
    `go build -ldflags "-X main.minversion \`date -u +.%Y%m%d%.H%M%S\`" service.go` – Sebastián Grignoli Jul 06 '12 at 04:49
  • This does not seem to work for me in Go 1.1.1: `$ go run -ldflags "-X main.xyz abc" main.go` -> `go run: no go files listed` – Felix Geisendörfer Jul 11 '13 at 05:57
  • 1
    @felixge I just tried it again with Go 1.1.1, and it worked for me. Anyway, "go run" is just for playing; use "go build" as in Sebastián's comment. – axw Jul 15 '13 at 01:02
  • If you're on Windows, here's one you can use from the command prompt: `go build -ldflags "-X main.minversion \"%date%\""`. This doesn't work on LiteIDE though. – Carl Oct 16 '13 at 23:30
  • 5
    goxc does this for you :) by default it compiles with -ldflags "-Xmain.VERSION x.x.x -Xmain.BUILD_DATE CurrentDateInISO8601", but you can configure those variable names if you like. See https://github.com/laher/goxc ... (disclaimer: I wrote goxc) – laher Nov 14 '13 at 20:51
  • Note: -ldflags -X will work for variables in the main package but not for packages. See https://groups.google.com/d/msg/golang-nuts/F6uxUrKZAsE/StdNt-CX3k4J – Sardonic Apr 04 '14 at 01:41
  • date -u +.%Y%m%d.%H%M%S – Blake Caldwell Dec 09 '14 at 19:43
  • 1
    I believe that the last sentence is not correct: a string cannot be nil in Go (when declared it just assumes the value of an empty string). Therefore `go build service.go` would work as well. – bcandrea Jun 26 '15 at 15:45
  • 7
    working example with new 1.5 syntax for adding the buildtime variable ```go build -ldflags "-X 'main.buildtime=$(date -u '+%Y-%m-%d %H:%M:%S')'"``` – xorpaul Oct 26 '15 at 16:17
  • The xyz variable must be exported, otherwise it doesn't work. – Maarten O. Nov 17 '15 at 11:33
  • @MaartenO. I just copy-pasted the example and it works fine with go 1.5. – axw Nov 18 '15 at 00:50
  • 34
    notice that full package name is required. `go build -ldflags "-X pkg.version=123"` won't work while `go build -ldflags "-X path/to/pkg.version=123"` work as expected. hope it helps. – csyangchen Nov 25 '15 at 09:04
  • Does this somehow work for a library. Let's say i want to use the library in another main. How can i make this ldflags valid while building the library? – Strubbl Jun 20 '17 at 19:37
  • 1
    @Strubbl -ldflags only applies when you create the final binary. You would have to supply that each time you create a binary using the library. – axw Jun 22 '17 at 00:29
  • It's convenient to overwrite globals initialized with a message like: the build did not provide this value. – user1212212 Oct 18 '17 at 17:27
  • 1
    @Strubbl yes this also works for a library. if the library is in your /vendor/ folder the path/to/pkg will be really long. e.g. I have a library that I call instrulog that is included in my background worker rc-nag-notification-scheduler-worker go build -ldflags "-X bitbucket.org/newyuinc/rc-nag-notification-scheduler-worker/vendor/bitbucket.org/newyuinc/instrulog.Version=bd48434-dev" – Sentient Dec 07 '17 at 23:38
  • in "-X main.xyz=abc" main is the package name or file_name or method name? – Pankaj Singh Jul 18 '18 at 16:43
  • 1
    @PankajSingh as mentioned in the -help output, it's the [package import path](https://golang.org/doc/code.html#ImportPaths). – axw Jul 20 '18 at 01:47
  • is it possible to initialize a struct's field (of type string) with this method? I want to logically group a few build values - and trying to set them with say "-X main.BuildInfo.GitHash=123abc7 -X main.BuildInfo.Unixtime=1541987669" has no effect on the struct i.e. the struct string fields have their _zero_ values. – colm.anseo Nov 12 '18 at 01:55
  • 1
    @colminator not directly, you would instead have to initialise the struct variable from package-level string vars. – axw Nov 13 '18 at 02:06
  • "The string variable must exist, it must be a variable, not a constant, and its value must not be initialized by a function call" https://github.com/golang/go/wiki/GcToolchainTricks – Sensei Feb 22 '19 at 19:31
  • I beleive you can actually initialize already initialized variables, too – andig Apr 30 '19 at 06:17
  • with Go 1.13+ modules, you need to specify the entire git module URL – Marcello DeSales Oct 30 '19 at 01:29
  • what if I use "replace" in go.mod. the `importpath` should be the original module or replaced one? – Jiang YD Feb 07 '21 at 12:26
42

Use ldflags to set variables in main package:

With file main.go:

package main

import "fmt"

var (
    version string
    build   string
)

func main() {
    fmt.Println("version=", version)
    fmt.Println("build=", build)
}

Then run:

go run \
  -ldflags "-X main.version=1.0.0 -X main.build=12082019" \ 
  main.go

Build:

go build -o mybinary \
  -ldflags "-X main.version=1.0.0 -X 'main.build=$(date)'" \ 
  main.go

Use ldflags to set variable in a non-main package:

With file config.go:

package config

import "fmt"

var (
    Version string
)

func LogVersion() {
    fmt.Println("version=", Version)
}

You will also need file main.go:

package main

import (
    "fmt"
    "github.com/user/repo/config"
}

func main() {
    config.LogVersion()
}

Build your binary first:

go build -o mybinary main.go 

Find the full path of variable name you want to set:

go tool nm <path_to_binary> | grep Version

Run and build the binary again but with the ldflags:

go run \
  -ldflags "-X github.com/user/repo/config.Version=1.0.0" \
  main.go --version       


go build -o mybinary \
  -ldflags "-X github.com/user/repo/config.Version=1.0.0" \
  main.go     

Inspired by https://github.com/golang/go/wiki/GcToolchainTricks#including-build-information-in-the-executable


Also if you are using goreleaser then read this https://goreleaser.com/environment/#using-the-mainversion :

Default wise GoReleaser sets three ldflags:

main.version: Current Git tag
main.commit: Current git commit SHA
main.date: Date according RFC3339


If you want to see this in action: https://github.com/hoto/fuzzy-repo-finder/blob/master/pkg/config/config.go

Andrzej Rehmann
  • 12,360
  • 7
  • 39
  • 38
34

Additionally I would like to post a small example how to use git and a makefile:

--- Makefile ----

# This how we want to name the binary output
BINARY=gomake

# These are the values we want to pass for VERSION and BUILD
# git tag 1.0.1
# git commit -am "One more change after the tags"
VERSION=`git describe --tags`
BUILD=`date +%FT%T%z`

# Setup the -ldflags option for go build here, interpolate the variable values
LDFLAGS_f1=-ldflags "-w -s -X main.Version=${VERSION} -X main.Build=${BUILD} -X main.Entry=f1"
LDFLAGS_f2=-ldflags "-w -s -X main.Version=${VERSION} -X main.Build=${BUILD} -X main.Entry=f2"

# Builds the project
build:
    go build ${LDFLAGS_f1} -o ${BINARY}_f1
    go build ${LDFLAGS_f2} -o ${BINARY}_f2

# Installs our project: copies binaries
install:
    go install ${LDFLAGS_f1}

# Cleans our project: deletes binaries
clean:
    if [ -f ${BINARY} ] ; then rm ${BINARY} ; fi

.PHONY: clean install

The make file will create two executables. One is executing function one, the other will take function two as main entry:

package main

import (
        "fmt"
)

var (

        Version string
        Build   string
        Entry   string

        funcs = map[string]func() {
                "f1":functionOne,"f2":functionTwo,
        }

)

func functionOne() {
    fmt.Println("This is function one")
}

func functionTwo() {
    fmt.Println("This is function two")
}

func main() {

        fmt.Println("Version: ", Version)
        fmt.Println("Build Time: ", Build)

    funcs[Entry]()

}

Then just run:

make

You will get:

mab@h2470988:~/projects/go/gomake/3/gomake$ ls -al
total 2020
drwxrwxr-x 3 mab mab    4096 Sep  7 22:41 .
drwxrwxr-x 3 mab mab    4096 Aug 16 10:00 ..
drwxrwxr-x 8 mab mab    4096 Aug 17 16:40 .git
-rwxrwxr-x 1 mab mab 1023488 Sep  7 22:41 gomake_f1
-rwxrwxr-x 1 mab mab 1023488 Sep  7 22:41 gomake_f2
-rw-rw-r-- 1 mab mab     399 Aug 16 10:21 main.go
-rw-rw-r-- 1 mab mab     810 Sep  7 22:41 Makefile
mab@h2470988:~/projects/go/gomake/3/gomake$ ./gomake_f1
Version:  1.0.1-1-gfb51187
Build Time:  2016-09-07T22:41:38+0200
This is function one
mab@h2470988:~/projects/go/gomake/3/gomake$ ./gomake_f2
Version:  1.0.1-1-gfb51187
Build Time:  2016-09-07T22:41:39+0200
This is function two
Literadix
  • 1,379
  • 1
  • 18
  • 31
  • 7
    Or simpler: just make two main in two different directories. This solution seems to be seriously overengineered. – dolmen Oct 27 '17 at 16:09
28

I had trouble using the -ldflags parameter when building my mixed command-line app and library project, so I ended up using a Makefile target to generate a Go source file containing my app's version and the build date:

BUILD_DATE := `date +%Y-%m-%d\ %H:%M`
VERSIONFILE := cmd/myapp/version.go

gensrc:
    rm -f $(VERSIONFILE)
    @echo "package main" > $(VERSIONFILE)
    @echo "const (" >> $(VERSIONFILE)
    @echo "  VERSION = \"1.0\"" >> $(VERSIONFILE)
    @echo "  BUILD_DATE = \"$(BUILD_DATE)\"" >> $(VERSIONFILE)
    @echo ")" >> $(VERSIONFILE)

In my init() method, I do this:

flag.Usage = func() {
    fmt.Fprintf(os.Stderr, "%s version %s\n", os.Args[0], VERSION)
    fmt.Fprintf(os.Stderr, "built %s\n", BUILD_DATE)
    fmt.Fprintln(os.Stderr, "usage:")
    flag.PrintDefaults()
}

If you wanted an atomically-increasing build number instead of a build date, however, you would probably need to create a local file that contained the last build number. Your Makefile would read the file contents into a variable, increment it, insert it in the version.go file instead of the date, and write the new build number back to the file.

pegli
  • 680
  • 8
  • 7
  • 2
    Nice solution. Still, I think I've found the reason for the -ldflags troubles. If the file containing variable being updated by -X isn't touched, then the compilation doesn't trigger and you have old version in the binary. My solution was to touch a small file containing only variable being reset via -ldflags "-X ..." – Wojciech Kaczmarek Jun 21 '17 at 23:23
13

On Windows OS given the program below

package main

import "fmt"

var (
    version string
    date    string
)

func main() {
    fmt.Printf("version=%s, date=%s", version, date)
}

You can build using

go build -ldflags "-X main.version=0.0.1 -X main.date=%date:~10,4%-%date:~4,2%-%date:~7,2%T%time:~0,2%:%time:~3,2%:%time:~6,2%"

Date format assumes your environment echo %date% is Fri 07/22/2016 and echo %time% is 16:21:52.88

Then the output will be: version=0.0.1, date=2016-07-22T16:21:52

Ostati
  • 4,623
  • 3
  • 44
  • 48
11

to use multi -ldflags:

$ go build -ldflags "-X name1=value1 -X name2=value2" -o path/to/output
Kimia Zhu
  • 209
  • 2
  • 2
0

Building on the other answers, with recent go versions it's also possible to write a buildid to an ELF section - though that's not so easily readable from within the program.

I write the same value to both, using something like the following:

BuildInfo:= "BUILD #x, branch @ rev built yymmdd hh:mm:ss"
// note the nested quotes "''" required to get a string with 
// spaces passed correctly to the underlying tool
ldFl := fmt.Sprintf("-X 'main.buildId=%s' -s -w '-buildid=%s'", BuildInfo, BuildInfo)
args := []string{
    "build",
    "-ldflags", ldFl,
    "-trimpath",
    "-gcflags", "-dwarf=false",
}
buildpath:="path/to/my/cmd"
args=append(args,buildpath)
buildCmd:=exec.Command("go", args...)

I use this with mage, a build tool written in go. You don't need the extra flags above, but I chose those to strip as much information as possible from release binaries.

(off topic: Mage requires a bit more upfront work than something like Make, but is much easier to extend/maintain than a make-based build system - plus you don't have to switch mental gears between go and some other syntax.)

Mark
  • 1,035
  • 2
  • 11
  • 24