I am so sorry, this post article is not completed yet. I forgot to finalize it even though I already have had sample code.

Hello every body, welcome to my blog!

This time I will share with you, how to generate go app version using go:generate.

Ok, without further ado, let’s get started.

In this tutorial, we’ll use a demo project as shown at below image: project structure

I will share the source code at the end of this article.

There are 4 parts that construct our demo project:

  1. cmd/cli.go, is the main app that will show its version and buildtime when it is executed using this command ./bin/cli -version
  2. bin, is a directory where the build will be stored
  3. internal/vcs/vcs.go, is shareable code which its main purpose is to generate version based git tag dan git commit
  4. version-generation-part, is, as a whole, where we set a version
    1. cmd/cmd.go, is where we declare version variable and set special comment //go:generate go run gen.go
    2. cmd/gen.go, is the code that is run by go:generate and contains the template for version.go
    3. cmd/version.go, is the file that generated by go:generate

Here are the general overview of using go generate to set version during build process:

  1. before we run go build, we run go generate ./... (see the Makefile)
  2. sas

OK,

I think that’s all for now. Hopefully this tutorial will be helpful for others.

Thanks a lot for watching. Have a great day. See you.

comments powered by Disqus