You have developed a CLI for your Go program. You even added the version sub-command following the useful semantic versioning: vMAJOR.MINOR.PATCH
A few days later, you discover an urgent bug.
You quickly fix the code, compile the code, release the new binary and tell your users to download the latest version.
But, some users are complaining the bug prevails. You ask them to run the
version sub-command to ensure their binary is up-to-date.
You realize you forgot to change the version. Solution? Automate the build process to inject the latest Git commit into your binary during the compilation time!
Define a new global variable that will be populated during the build using the
-ldflags Go feature, and it's going to be part of the version output.
Add two new variables into your Makefile:
git rev-listcommand, listing commit objects in reverse chronological order
And pass the
GitCommit argument into your program using the
make install and enjoy the detailed
You can see the full source code with the changes here: https://github.com/web3coach/the-blockchain-bar/commit/fecdd0fe12a6d37bcd520da2202c6093f32a241f
Hi, I am writing an eBook on how to build a peer-to-peer system in Go from scratch!
Follow how the main protagonist Andrej, a web developer by day and a bartender by night, learns real-world use of blockchain through the growth of his new business venture: "The Blockchain Bar".