Skip to content
Latchkey

go build Command: Compile Go in CI

go build compiles Go packages and their dependencies into an executable.

go build is the core compile step in Go CI pipelines. It resolves modules, compiles, and links a binary. A small set of flags covers output naming, version stamping, build tags, and cross-compilation.

Common flags

  • -o NAME - write the output binary to NAME (file or directory)
  • -ldflags "-s -w" - pass linker flags; strip symbols / set version vars
  • -tags "netgo osusergo" - enable build tags / conditional files
  • -race - build with the data-race detector
  • -trimpath - remove local file system paths from the binary (reproducible builds)
  • -v - print package names as they are compiled
  • GOOS=linux GOARCH=arm64 - environment vars to cross-compile

Example in CI

Cross-compile a stripped, reproducible Linux arm64 binary with a stamped version:

shell
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -trimpath -ldflags "-s -w -X main.version=\${GIT_SHA}" -o bin/app ./cmd/app

Common errors in CI

  • cannot find package "X" in any of ... - missing module or wrong import path
  • missing go.sum entry for module ... - run go mod tidy/go mod download
  • undefined: X - symbol not declared, often a build-tag-excluded file
  • build constraints exclude all Go files in ... - no file matches the active GOOS/tags

Key takeaways

  • -o names the binary; -ldflags "-X ..." stamps version metadata.
  • Set GOOS/GOARCH (and CGO_ENABLED=0) to cross-compile static binaries.
  • Most CI failures are missing go.sum entries or unmatched build constraints.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →