Set variables at build time in Golang

golang, programming

For some CLI tools I use regularly I noticed that when I run the version flag get some version info that includes a git commit hash or a git tag to the command line:

1> kubectl version
2Client Version: version.Info{Major:"1", Minor:"16", GitVersion:"v1.16.3", GitCommit:"b3cbbae08ec52a7fc73d334838e18d17e8512749", GitTreeState:"clean", BuildDate:"2019-11-13T11:23:11Z", GoVersion:"go1.12.12", Compiler:"gc", Platform:"linux/amd64"}
3Server Version: version.Info{Major:"1", Minor:"16", GitVersion:"v1.16.2", GitCommit:"c97fe5036ef3df2967d086711e6c0c405941e14b", GitTreeState:"clean", BuildDate:"2019-10-15T19:09:08Z", GoVersion:"go1.12.10", Compiler:"gc", Platform:"linux/amd64"}

I discovered a nice way to do this for go programs. You can specify a value for a global variable at build time using the -ldflags "-X" flag.

For this program:

1package main
2
3import "fmt"
4
5var Version = "dev"
6
7func main(){
8  fmt.Printf("I'm version <%s>", Version)
9}

You can use this one liner that builds your go program while setting the Version variable to the sha hash of the latest commit.

1> go build -ldflags "-X main.Version=$(git rev-parse HEAD)" -o hello_app main.go
2> hello_app
3I'm version ba83f7c418c669f705a3cce0c58a1f9129a3de14

Note that you have to specify the full path of the package in your project, for example:

1# Your project looks like
2# - main.go <- module `github.com/myrepo/test`
3# - version <- `github.com/myrepo/test/version`
4#   - sha.go <- has `var Version = "dev"`
5> go build -ldflags "-X github.com/myrepo/test/version.Version=$(git rev-parse HEAD)" -o hello_app main.go
6> hello_app
7I'm version ba83f7c418c669f705a3cce0c58a1f9129a3de14

Articles from blogs I follow

My plans at FOSDEM: SourceHut, Hare, and Helios

FOSDEM is right around the corner, and finally in person after long years of dealing with COVID. I’ll be there again this year, and I’m looking forward to it! I have four slots on the schedule (wow! Thanks for arranging these, FOSDEM team) and I’ll be talkin…

via Drew DeVault's blog January 24, 2023

YSK: Google allows spoofing news headlines in search results

A minor scandal unfolding in the Swedish election highlights a way to influence news narratives: Google allows you to set headlines for news articles in search results by paying for adwords placements of legitimate articles. This is being used by political …

via Jacob Davis-Hansson September 9, 2022

Going multipath without Multipath TCP

Going multipath without Multipath TCP Gigabit ethernet has been around for a long time, it’s so ubiquitous that there is a very strong chance that if you have a RJ-45 port on your compu

via benjojo blog February 24, 2022

Generated by openring