Skip to content

Instantly share code, notes, and snippets.

@jxsl13
Forked from awalterschulze/.gitignore
Last active July 7, 2021 15:54
Show Gist options
  • Save jxsl13/528eaa437d90976e02fad9d2536388ac to your computer and use it in GitHub Desktop.
Save jxsl13/528eaa437d90976e02fad9d2536388ac 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)
}
SHORT_HASH := $(shell git rev-parse --short HEAD)
VERSION := $(shell git describe --tags)
BUILDTIME := $(shell date --rfc-email)
GOLDFLAGS += -X 'github.com/jxsl13/simple-configo.ShortHash=$(SHORT_HASH)'
GOLDFLAGS += -X 'github.com/jxsl13/simple-configo.Version=$(VERSION)'
GOLDFLAGS += -X 'module/path/in/gomod/main.Expires=true'
GOLDFLAGS += -X 'main.Expires=true'
GOFLAGS = -ldflags "$(GOLDFLAGS)"
run: build
./mybinary
build:
go build -o mybinary $(GOFLAGS) .
release:
env GOOS=linux GOARCH=amd64 go build $(GOFLAGS) .
env GOOS=windows GOARCH=amd64 go build $(GOFLAGS) .
go install $(GOFLAGS) .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment