Skip to content

Instantly share code, notes, and snippets.

@jackgris
Created August 2, 2023 21:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jackgris/8f39891fbcc5847671c03cdfd8a6f440 to your computer and use it in GitHub Desktop.
Save jackgris/8f39891fbcc5847671c03cdfd8a6f440 to your computer and use it in GitHub Desktop.
Versioning Go binaries

With a code like this:

package main

import "fmt"

var (
	version = "dev"
	commit  = "n/a"
	date    = "n/a"
)

func main() {
	fmt.Printf("%s, %s, %s\n", version, commit, date)
}

You can run a command like this:

go build --ldflags "-X 'main.version=$(git describe --tags)' -X 'main.date=$(date +%F)' -X 'main.commit=$(git rev-parse HEAD)'" app.go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment