I wrote up something very similar to this back in May 2020.
I never understood why this doesn’t work like in C where you can define constants this way, not variables.
Build times, perhaps. It allows objects in the build cache to be reused and modified at link time, rather than needing to recompile.
Cool! I definitely would have used go gen to do something like this, and I would have been forever a little unhappy with the results. This is way slicker!
Ah! I wrote a thing that you could use. buildconstants. I was aware of the linker variables but wanted something pure go/scriptable.
I’m super proud that i managed to get it open source so if you have any suggestions/bug reports, let me know.
The thockin/go-build-template: A Makefile/Dockerfile example for Go projects uses the approach outlined in the article to derive the version from git repo and set it in the version package.
I’ve been using the template for my personal projects the ones at $WORK. Highly recommend it for anyone starting their project from scratch. The repo is well structured and provides a great (build) baseline.