Skip to content

Instantly share code, notes, and snippets.

@jaymecd
Last active March 16, 2024 16:34
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jaymecd/31574e7efcd159aecd01 to your computer and use it in GitHub Desktop.
Save jaymecd/31574e7efcd159aecd01 to your computer and use it in GitHub Desktop.
Golang Auto Build Versioning
go run -ldflags "-s \
  -X main.Version=1.0.2 \
  -X main.BuildTime=`TZ=UTC date -u '+%Y-%m-%dT%H:%M:%SZ'` \
  -X main.GitHash=`git rev-parse HEAD`" \
  myapp.go

Adopted from www.atatus.com

package main
import "fmt"
var (
Version = "undefined"
BuildTime = "undefined"
GitHash = "undefined"
)
func main() {
fmt.Printf("Version : %s\n", Version)
fmt.Printf("Git Hash : %s\n", GitHash)
fmt.Printf("Build Time : %s\n", BuildTime)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment