golang include sha information in your binary

Developer from somewhere

Problem: you want to include the git SHA in your binary

Solution: Assuming the following code:

package main

var appVersion string

func main() {
  fmt.Println("running version:", appVersion)
}
  • you can run the binary like this: go run -ldflags “-X main.appVersion=$(git rev-parse HEAD)” main.go
  • you can build it like this: go build -ldflags “-X main.appVersion=$(git rev-parse HEAD)” main.go