debug.BuildInfo - Main.Version = devel

Hi

I’m trying to get “version” from debug.BuildInfo:

package main

import (
	"fmt"
	"runtime/debug"
)

func main() {
	bi, ok := debug.ReadBuildInfo()
	if !ok {
		fmt.Println("not ok")
		return
	}
	fmt.Printf("Version: %s\n", bi.Main.Version)
}

Output:

Version: (devel)

I have tried with versions:
go1.17.5
go1.18beta1

I got the same result, with both versions.

Does it exist any way for go compiler to update Main.Version to git tag/go.mod-version (without using -ldflags="-X path.to.variable=value")?

In Go 1.18beta1 this should work, provided your code is in a module, and this has been tagged in git.

Hi

I still got version “(devel)”

package main

import (
	"fmt"
	"runtime/debug"
)

func main() {
	bi, ok := debug.ReadBuildInfo()
	if !ok {
		panic("not ok")
	}
	fmt.Printf("%+v", bi)
}

Output:

&{GoVersion:go1.18beta1 Path:play.com/buildinfo Main:{Path:play.com/buildinfo Version:(devel) Sum: Replace:<nil>} Deps:[] Settings:[{Key:-compiler Value:gc} {Key:CGO_ENABLED Value:0} {Key:GOARCH Value:amd64} {Key:GOOS Value:darwin} {Key:GOAMD64 Value:v1} {Key:vcs Value:git} {Key:vcs.revision Value:bd21e4e6b920b1277fc1cbb3e02612220b0d4989} {Key:vcs.time Value:2022-01-11T14:46:27Z} {Key:vcs.modified Value:false}]}

Running:

git log
commit bd21e4e6b920b1277fc1cbb3e02612220b0d4989 (HEAD -> main, tag: v1.0.0)
Author: Micke
Date:   Tue Jan 11 15:46:27 2022 +0100

    intial