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 compiledGOOS=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/appCommon 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
-onames the binary;-ldflags "-X ..."stamps version metadata.- Set
GOOS/GOARCH(andCGO_ENABLED=0) to cross-compile static binaries. - Most CI failures are missing go.sum entries or unmatched build constraints.
Related guides
go vet Command: Static Checks in CIgo vet reports suspicious Go constructs. Reference for ./..., -vettool, and the printf/struct-tag/lock-copy i…
cargo Command: Build & Test Rust in CIcargo is the Rust build tool and package manager. Reference for build, test, --release, --locked, --all-featu…
make Command: Run Builds in CImake runs builds from a Makefile. Reference for -j, -C, -f, -B, -k, VAR=value overrides, and the Makefile err…