1. 22
  1.  

  2. 4

    I wrote up something very similar to this back in May 2020.

    1. 2

      I never understood why this doesn’t work like in C where you can define constants this way, not variables.

      1. 3

        Build times, perhaps. It allows objects in the build cache to be reused and modified at link time, rather than needing to recompile.

      2. 2

        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!

        1. 2

          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.

          1. 1

            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.