1

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?

Elliot Chance
  • 5,526
  • 10
  • 49
  • 80
  • i am not very clear about the problem, see if this help http://stackoverflow.com/a/42766877/3435777 – zzn Apr 26 '17 at 01:51
  • `gopkg.in` is used as a redirect service to allow `go get` to fetch a specific version, but I want to always get the HEAD, but capture the git tag when `go build` runs so it can automatically encode the version into the app without me having to update it. – Elliot Chance Apr 26 '17 at 01:53
  • 1
    Possible duplicate of [How to do "go get" on a specific tag of a github repository](http://stackoverflow.com/questions/30188499/how-to-do-go-get-on-a-specific-tag-of-a-github-repository) – Chris Halcrow Apr 26 '17 at 08:12
  • Not a duplicate. That question is when you want to request a specific version. I want the latest version, but when it builds it uses the fetched version. – Elliot Chance Apr 28 '17 at 01:30

2 Answers2

2

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.

Ralph
  • 31,584
  • 38
  • 145
  • 282
0

No this is not possible. (Note that go get supports other SCM than git too).

Volker
  • 40,468
  • 7
  • 81
  • 87