Like many others, there are occasions when and I want to store the value from the environment, like the current
build number or Git commit hash, in the source code of a software package so that I can display it on startup
or when invoked with a command line switch. This information is only available during the build
phase of the application, so something will need to be added to the build process to inject this value once it is
known. This is relatively easy to do in C by using the
-D switch to the compiler to declare the value as a macro.
Even in Java this can be done using Maven resource filtering.
However, I was unaware of how this was done in Go.
To date, I have been using a simple form of code generation that involved passing
sed over a source file with a
dummy string constant.
sed would be configured to replace that constant
with the version number or Git hash prior to the Go application being built. This worked, but it was a little bit inelegant.
I have since learnt, via this Stack Overflow answer, that it’s possible to set the
initial value of a Go variable directly from the build command.
The process is not as nice as it could be, as it involves actually setting flags to the linker, but it works
reasonably well and is much nicer than doing things with