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.