Populating go vars at build time
When we build go executables, one common command is the
version command. It should tell you the commit, version number etc of the released executable. However, we don’t want to hardcode these in code. We want to populate these variables at build time. Here is a common pattern that will let you do this.
Let’s assume you have a
main.go that imports a package
version and prints out the output of
version.go is a very simple file:
In order to populate the
buildVersion, buildGitRevision, buildTag variables at build time, we will be using the
-ldflags argument to the
go build tool when building. Assuming you are using a
Makefile in your root to build your project, add a target like this:
Now you can build your project via
make build. If you run the generated
main executable, you will see that the version, tag and revision values are taken from the Makefile i.e. at build time.
You can view a fully working example on github.