Code-Memo

Builds, cross-compilation, and static binaries

Go is popular for DevOps because it can produce a single executable for many OS/arch targets.

Basic build

go build ./...
go build -o mytool ./cmd/mytool

Cross compilation (OS/ARCH)

GOOS=linux  GOARCH=amd64 go build -o mytool-linux-amd64 ./cmd/mytool
GOOS=linux  GOARCH=arm64 go build -o mytool-linux-arm64 ./cmd/mytool
GOOS=darwin GOARCH=arm64 go build -o mytool-darwin-arm64 ./cmd/mytool
GOOS=windows GOARCH=amd64 go build -o mytool-windows-amd64.exe ./cmd/mytool

Reproducible builds (common flags)

go build -trimpath -buildvcs=false ./cmd/mytool

Injecting version at build time

If you have a package variable like version.Version, you can set it:

go build -ldflags "-X example.com/myrepo/internal/version.Version=1.2.3" ./cmd/mytool

Static binaries (Linux)

Static linking depends on CGO usage and your dependencies. A common baseline:

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o mytool ./cmd/mytool

If your program requires CGO (some DNS/OS integrations, sqlite, etc.), fully static builds may not be possible without extra tooling.

Multi-platform CI builds

Prefer a build matrix over ad-hoc scripts. For Go, it’s often: