Created
July 26, 2024 21:17
-
-
Save dlisboa/a007c59e0725f9211c93a47e7ac5b392 to your computer and use it in GitHub Desktop.
Set global variables on build time in Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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