Reputation: 5746
I would like to add a --version
option to my Go binary, c2go.
I know traditionally this would be hardcoded into the binary but since it's alpha quality software and it's being updated very often I'd like to capture the version when the go get
builds the executable (from the git tag).
Is this possible?
Upvotes: 1
Views: 1270
Reputation: 32294
This does not exactly answer your question, but the way I solved a similar need is the following.
In my main
package, I define the following variables:
var (
buildInfo string
buildStamp = "No BuildStamp provided"
gitHash = "No GitHash provided"
version = "No Version provided"
)
and in my main
function, I execute the following code:
if buildInfo != "" {
parts := strings.Split(buildInfo, "|")
if len(parts) >= 3 {
buildStamp = parts[0]
gitHash = parts[1]
version = parts[2]
}
}
I then build my application with the following bash
(Linux) shell script:
#!/bin/sh
cd "${0%/*}"
buildInfo="`date -u '+%Y-%m-%dT%TZ'`|`git describe --always --long`|`git tag | tail -1`"
go build -ldflags "-X main.buildInfo=${buildInfo} -s -w" ./cmd/...
You will need to adjust the script for Windows and MacOS.
Hope that helps.
Upvotes: 2
Reputation: 42431
No this is not possible. (Note that go get supports other SCM than git too).
Upvotes: 0