Skip to content

Instantly share code, notes, and snippets.

@dlisboa
Created July 26, 2024 21:17
Show Gist options
  • Save dlisboa/a007c59e0725f9211c93a47e7ac5b392 to your computer and use it in GitHub Desktop.
Save dlisboa/a007c59e0725f9211c93a47e7ac5b392 to your computer and use it in GitHub Desktop.
Set global variables on build time in Go
package main
import "fmt"
// set these vars on build time:
// go build -ldflags "-X main.version=v1.2.3 -X main.commit=$(git rev-parse --short HEAD) -X main.date=$(date -u '+%Y-%m-%dT%H:%M:%S')" -o foo -v ./foo.go
var (
service = "some-service"
version string
commit string
date string
)
func main() {
fmt.Printf("running %s on version %s, commit %s, build date %s\n",
service, version, commit, date)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment