Build information and target binaries
Build information and target binaries
An artifact is a particular binary, not just a source commit. Record its target OS and architecture, compiler version, module dependencies and build flags. go version -m ./app reads embedded Go build information without running the app; go version alone describes the active toolchain. In a running Go program, runtime/debug.ReadBuildInfo exposes information when present. Cross-compilation produces an artifact for a different target, not evidence that it runs there.
package main
import ( "fmt" "runtime" "runtime/debug" "strings")
func main() { info, ok := debug.ReadBuildInfo() if !ok { panic("no build info") } fmt.Println(strings.HasPrefix(info.GoVersion, "go")) fmt.Println(runtime.GOOS != "", runtime.GOARCH != "")}GoVersion identifies the compiler recorded in the running binary, not the version of a different go command on PATH. The runtime constants describe its compilation target. Module path, version and VCS settings may be absent or differ according to build mode; do not assume every executable has a clean revision. Save the build command and verify the source state separately.
Expected output:
truetrue trueCheck embedded target settings
package main
import ( "fmt" "runtime" "runtime/debug")
func main() { info, ok := debug.ReadBuildInfo() if !ok { panic("no build info") } m := map[string]string{} for _, s := range info.Settings { m[s.Key] = s.Value } fmt.Println(m["GOOS"] == runtime.GOOS, m["GOARCH"] == runtime.GOARCH)}In a module with a main package, run CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o app-arm64 . and inspect go version -m app-arm64. On a compatible Linux arm64 machine, run its tests or the binary separately. cgo requires a compatible C toolchain for cross-compilation; disabling it may change package behavior or remove support. Record an artifact hash for integrity, but a hash alone is not provenance or a signature.
Expected output:
true trueTry it locally
Write a helper that accepts target settings and refuses a missing or mismatched architecture. Test a valid target and a missing one.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Toolchain and compatibility 03: Build information and target binaries
Check the lesson objective before moving on.
What does go version -m inspect?
What does a successful cross-build establish?