Go Lang Developer Tips

The following are a list of go lang tips I want to easily find in the future.

  1. How to Embedding Commit Ids in Go code from Red Hat

Using -ldflags

The most common way is by using a string variable, a value populated at build time via flags.

For example:

var Commit string

Copy snippet

go build -ldflags="-X main.Commit=$(git rev-parse HEAD)"

Copy snippet

The disadvantage here is that you need to remember this syntax and run it every time you build your code. You can make this easier by using Makefiles to do that for you.