Injecting build time variables

September 30, 2019
golang | build | git

A common use-case for build-time variables is to inject the Git version information in the binary. There are several approaches on how to do this, but this one is the one I found the most useful one.

Let’s start with creating a sample project using Go modules:

$ mkdir example-go-build-time-variables
$ cd example-go-build-time-variables
$ go mod init
go: creating new go.mod: module

As we want to play with the Git revision and branch info, let’s also initialize it as a Git repository:

$ echo "# example-go-build-time-variables" >>
$ git init
Initialized empty Git repository in ~/example-go-build-time-variables/.git/
$ git add .
$ git commit -m "first commit"
[master (root-commit) 47c55c2] first commit
 2 files changed, 4 insertions(+)
 create mode 100644
 create mode 100644 go.mod

Let’s start with creating the most simple app we can think of by adding a main.go file with the following contents:


package main

import (

func main() {
    fmt.Println("Hello stranger!")

Let’s also create a package called version in which the version information is going to be stored:


package version

// GitRevision will be injected with the current git commit hash
var GitRevision string

// GitBranch will be injected with the current git branch name
var GitBranch string

I prefer to create a non-related package just containing this information. This makes it easy to access the version information from anywhere.

We can then update the main.go file to use this information:


package main

import (


func main() {
	fmt.Println("Git Revision:", version.GitRevision)
	fmt.Println("Git Branch:", version.GitBranch)

If we build and run it, you’ll get the following output:

$ go build -o example-go-build-time-variables .
$ ./example-go-build-time-variables
Git Revision: 
Git Branch: 

That’s the expected behaviour as we didn’t inject the version information yet.

To inject the information, you need to specify the -ldflags option during the go build process. You can test it manually by changing the build command to:

$ go build -o example-go-build-time-variables -ldflags "-X -X" .
$ ./example-go-build-time-variables
Git Revision: revision
Git Branch: branch 

Now, the only thing we need to do is to get the proper Git revision and branch name. The revision can be found with the following command:

$ git rev-parse --short HEAD

The branch name is obtained by the following command:

$ git rev-parse --abbrev-ref HEAD | tr -d '\040\011\012\015\n'

To make it easier, I usually combine everything in a simple Makefile which looks as follows:

APPNAME := example-go-build-time-variables

REVISION := $(shell git rev-parse --short HEAD)
BRANCH := $(shell git rev-parse --abbrev-ref HEAD | tr -d '\040\011\012\015\n')

	go build -ldflags "-X $(PACKAGE).GitRevision=$(REVISION) -X $(PACKAGE).GitBranch=$(BRANCH)" -o $(APPNAME)
run: build

You can then simply run either make build to create the build or make run to build and run the application:

make run
go build -ldflags "-X -X" -o example-go-build-time-variables
./example-go-build-time-variables server
Git Revision: 47c55c2
Git Branch: master

The complete source code for this example can be found here.