TExcelTAD
Home › Go Modules in Depth › Version Stamping & Build Info

Version Stamping & Build Info

"Which version is running in production?" should never need guesswork. Go binaries can answer it themselves in two ways: values you inject at link time with -ldflags -X, and the build information the go command embeds automatically (module versions, build settings, Git revision), which runtime/debug.ReadBuildInfo and go version -m read back.

-ldflags -X: set a string at link time

-X importpath.name=value sets a package-level string variable (not a constant, and not initialised by a function call) when linking. For package main, the import path is main:

go
var (
	version = "dev"   // overwritten by -ldflags "-X main.version=v1.2.0"
	commit  = "none"
)

Everything a binary knows about itself

This session commits a small program to Git, tags it v1.2.0, and builds it twice: plainly, then with injected values.

bash · try it yourself
mkdir stamp && cd stamp
git init -q
go mod init example.com/stamp 2>/dev/null
cat > main.go <<'SRC'
package main

import (
	"fmt"
	"runtime/debug"

	"github.com/google/uuid"
)

var (
	version = "dev"
	commit  = "none"
)

func main() {
	fmt.Println("version:", version, "commit:", commit)
	_ = uuid.NewSHA1(uuid.NameSpaceURL, []byte("stamp"))
	info, ok := debug.ReadBuildInfo()
	if !ok {
		return
	}
	fmt.Println("main module:", info.Main.Path, info.Main.Version)
	for _, d := range info.Deps {
		fmt.Println("dep:", d.Path, d.Version)
	}
	for _, s := range info.Settings {
		switch s.Key {
		case "vcs.revision", "vcs.modified", "CGO_ENABLED", "-ldflags":
			fmt.Printf("setting: %s=%s\n", s.Key, s.Value)
		}
	}
}
SRC
go mod tidy 2>/dev/null
echo "app" > .gitignore
git add . && git commit -qm "first release" && git tag v1.2.0
go build -o app . && ./app
echo "---"
go build -ldflags "-X main.version=$(git describe --tags) -X main.commit=$(git rev-parse --short HEAD)" -o app .
./app
version: dev commit: none
main module: example.com/stamp v1.2.0
dep: github.com/google/uuid v1.6.0
setting: CGO_ENABLED=1
setting: vcs.revision=c429cca6029f208d11293c8fcac8d4e14bb9ac6d
setting: vcs.modified=false
---
version: v1.2.0 commit: c429cca
main module: example.com/stamp v1.2.0
dep: github.com/google/uuid v1.6.0
setting: -ldflags=-X main.version=v1.2.0 -X main.commit=c429cca
setting: CGO_ENABLED=1
setting: vcs.revision=c429cca6029f208d11293c8fcac8d4e14bb9ac6d
setting: vcs.modified=false

Even the plain build knew a lot:

  • info.Main.Version is v1.2.0. Since Go 1.24, go build derives the main module's version from the Git tag on the current commit (a pseudo-version between tags, with +dirty when there are uncommitted changes). Before 1.24 it was always (devel).
  • vcs.revision and vcs.modified come from Git (-buildvcs, on by default when building inside a repository with the git binary available). The commit hash is reproducible here because the course's Git uses fixed dates.
  • Deps lists every module compiled in, with versions: an SBOM in miniature.

The -X values add what the go command can't know: a release name your CI chose, a build number, a channel. Many projects use both: -X for the human-facing --version string, build info for forensics.

go version -m: read any Go binary

You don't need the program's cooperation. go version -m prints the embedded build info of any Go binary, including ones you didn't build:

bash · try it yourself
mkdir stamp && cd stamp
git init -q
go mod init example.com/stamp 2>/dev/null
printf 'package main\n\nimport "github.com/google/uuid"\n\nfunc main() { println(uuid.Nil.String()) }\n' > main.go
go mod tidy 2>/dev/null
echo "app" > .gitignore
git add . && git commit -qm "first release" && git tag v1.2.0
CGO_ENABLED=0 go build -trimpath -ldflags "-X main.version=v1.2.0" -o app .
go version -m app
echo "--- after an uncommitted edit"
echo "// wip" >> main.go
go build -o app . && go version -m app | grep -E 'mod\s|vcs.modified'
echo "--- -buildvcs=false"
go build -buildvcs=false -o app . && go version -m app | grep -cE 'vcs' || true
app: go1.27.3
	path	example.com/stamp
	mod	example.com/stamp	v1.2.0
	dep	github.com/google/uuid	v1.6.0	h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
	build	-buildmode=exe
	build	-compiler=gc
	build	-trimpath=true
	build	CGO_ENABLED=0
	build	GOARCH=amd64
	build	GOOS=linux
	build	GOAMD64=v1
	build	vcs=git
	build	vcs.revision=3780288ad5b2809187b8803abaed0c4714fbbeec
	build	vcs.time=2026-01-15T10:00:00Z
	build	vcs.modified=false
--- after an uncommitted edit
	mod	example.com/stamp	v1.2.0+dirty
	build	vcs.modified=true
--- -buildvcs=false
0

One detail: this build used -trimpath, and the -ldflags setting is missing from the list. With -trimpath the go command leaves -ldflags out of the build info, because linker flags often contain local paths; the -X values are still in the binary. The dep line even carries the go.sum hash of each dependency, which is what govulncheck uses to scan binaries (govulncheck -mode=binary app). With -buildvcs=false (or outside a repository, or without git installed, as in many Docker builds that only COPY the source) there are no vcs lines, and the version falls back to (devel).

Note: go run and go test don't stamp VCS information (it would slow down every run), so ReadBuildInfo there shows (devel). Test your --version output on a binary from go build.

Patterns that work

  • Put version variables in a small package (internal/buildinfo) and set them with -X example.com/app/internal/buildinfo.Version=…; the full import path is required for non-main packages.
  • Never stamp the current time unless you must: it makes every build different. Use the commit time (vcs.time, or git log -1 --format=%cI) instead; that keeps builds reproducible.
  • Copy .git into Docker build contexts (or pass the values with -X), otherwise the binary has no VCS information.
  • Add .gitignore entries for build outputs: an untracked binary in the work tree is enough to mark the build +dirty.

Test yourself

Which variable can -ldflags "-X main.version=v1.2.0" set?

  1. const version = "dev"
  2. var version = "dev" in package main
  3. var version = getVersion()
  4. var version int

What does go version -m ./app show?

  1. Only the Go version
  2. The embedded build info: main module, dependency versions with hashes, build flags and VCS revision
  3. The source code
  4. The go.mod file of the current directory

Why might info.Main.Version show v1.2.0+dirty?

  1. The tag is invalid
  2. The work tree has uncommitted changes (including untracked build outputs)
  3. go.sum is missing
  4. cgo was enabled