2016-06-28 48 views
7

Sto usando i tag git per iniettare versioni nei miei programmi go, ad es. in un Makefile:Versione dinamica da git con go get

VERSION = $(shell git describe --always --dirty) 

github_pki: main.go 
    CGO_ENABLED=0 GOOS=linux \ 
     go build -a \ 
      -ldflags="-X main.version=$(VERSION)" \ 
     -installsuffix cgo -o [email protected] $< 

Dove version è definito in main.go come:

var version = "undefined" 

Questa grande opera utilizzando make, ma non quando si utilizza go get o go build. C'è un modo per fare in modo che questo ldflags funzioni senza utilizzare un sistema di generazione esterno (ad esempio con go build/go get)?

+0

Non secondo la documentazione. Tuttavia è possibile utilizzare l'attributo [ident git] (https://git-scm.com/docs/gitattributes#__code_ident_code) per aggiungere almeno l'hash git di un file nel file. –

risposta

-1

È il seguente che desideri?

VERSION=`git describe --always --dirty`; \ 
    CGO_ENABLED=0 GOOS=linux \ 
    go build -a \ 
     -ldflags="-X main.version=$(VERSION)" \ 
     -installsuffix cgo -o github_pki main.go