Skip to content

Instantly share code, notes, and snippets.

@awalterschulze
Last active September 2, 2022 10:42
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save awalterschulze/d0f217db28e17b2ee56e8e8a4e68c28e to your computer and use it in GitHub Desktop.
Save awalterschulze/d0f217db28e17b2ee56e8e8a4e68c28e to your computer and use it in GitHub Desktop.
compile git version inside go binary

Compile git version inside go binary

package main
import "fmt"
var Version string
var Buildtime string
func main() {
fmt.Printf("Version: %s\n", Version)
fmt.Printf("Buildtime: %s\n", Buildtime)
}
VERSION := $(shell git rev-parse --short HEAD)
BUILDTIME := $(shell date -u '+%Y-%m-%dT%H:%M:%SZ')
GOLDFLAGS += -X main.Version=$(VERSION)
GOLDFLAGS += -X main.Buildtime=$(BUILDTIME)
GOFLAGS = -ldflags "$(GOLDFLAGS)"
run: build
./mybinary
build:
go build -o mybinary $(GOFLAGS) .
@chenlujjj
Copy link

nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment