The normal process should no longer involve any ldflags, starting with Go 1.18 (Q4 2021/Q1 2022).
See issue 37475 and CL 356013
The go command now embeds version control information in binaries including the currently checked-out revision and a flag indicating whether edited or untracked files are present.
Version control information is embedded if the go command is invoked in a directory within a Git or Mercurial repository, and the main package and its containing main module are in the same repository.
This information may be omitted using the flag -buildvcs=false.
Additionally, the go command embeds information about:
- the build including build and tool tags (set with
-tags),
- compiler,
- assembler, and
- linker flags (like
-gcflags),
- whether
cgo was enabled, and if it was, the values of the cgo environment variables (like CGO_CFLAGS).
This information may be omitted using the flag -buildinfo=false.
Both VCS and build information may be read together with module information using:
go version -m file or
runtime/debug.ReadBuildInfo (for the currently running binary) or
- the new
debug/buildinfo package.
CL 373875 adds
- The
BuildInfo struct has new fields with additional information about how the binary was built.
- The new
GoVersion field holds the version of Go used to build the binary.
- The new
Settings field is a slice of BuildSettings structs holding key/value pairs describing the build.