Cross-compilation is the cheapest superpower in Go. The toolchain ships every supported target out of the box — building a Linux ARM64 binary on an Intel Mac is one environment variable away. The hard part is keeping the builds reproducible when you ship to multiple architectures simultaneously. This article covers the build matrix, the gotchas around CGO and net, and how goreleaser automates the whole thing.
How to verify
Confirm the toolchain supports the targets you need:
go version
go tool dist list | head -20
go env GOOS GOARCH CGO_ENABLED
file ./bin/myapp 2>/dev/null
goreleaser --version 2>/dev/null || echo "goreleaser not installed"
go tool dist list enumerates every GOOS/GOARCH combo the local toolchain can target. If a target you expect (linux/arm64, darwin/arm64) is missing, the Go install is incomplete — reinstall from the official tarball.
What’s happening
A Go build is compile + link, and both stages are aware of GOOS (target operating system) and GOARCH (target architecture). With pure Go code and CGO_ENABLED=0, every target is just an environment variable change away — no cross-compiler, no toolchain shenanigans, no QEMU.
CGO is where it stops being free. import "C" in any package — yours or transitively — means the build needs a C compiler that targets the destination. Building Linux ARM64 binaries from Linux amd64 then requires gcc-aarch64-linux-gnu (or use zig cc as the cross-linker, which is cleaner). Most stdlib packages are pure Go, but os/user and net historically used CGO — modern Go gives you -tags=osusergo,netgo flags to force the pure-Go versions and re-enable cross-compilation.
The other half is reproducibility. Two builds of the same commit should produce bit-identical binaries. -trimpath removes local paths. Embedded VCS info (Go 1.18+) bakes the commit hash and modification flag into the binary, which is good for observability but means a dirty working tree produces a different binary than a clean one — keep CI on a clean checkout.
The procedure
-
Build a single non-native target. Linux ARM64 binary from any amd64 host:
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 \ go build -trimpath -ldflags="-s -w" \ -o bin/myapp-linux-arm64 ./cmd/server file bin/myapp-linux-arm64 # ELF 64-bit LSB executable, ARM aarch64, ... statically linked -
Scripted build matrix for the common Linux targets:
cat > build.sh <<'EOF' #!/usr/bin/env bash set -euo pipefail VERSION=$(git describe --tags --always --dirty) mkdir -p dist for combo in linux/amd64 linux/arm64 darwin/amd64 darwin/arm64; do GOOS=${combo%/*} GOARCH=${combo#*/} out="dist/myapp-${VERSION}-${GOOS}-${GOARCH}" [[ $GOOS == windows ]] && out="${out}.exe" echo "Building $out" CGO_ENABLED=0 GOOS=$GOOS GOARCH=$GOARCH \ go build -trimpath \ -ldflags="-s -w -X main.version=${VERSION}" \ -o "$out" ./cmd/server done EOF chmod +x build.sh ./build.sh -
For CGO-heavy projects (image processing, ML, anything that links against system libs), use
zig ccas the cross-compiler:sudo snap install zig --classic --beta # or download from ziglang.org CGO_ENABLED=1 GOOS=linux GOARCH=arm64 \ CC="zig cc -target aarch64-linux-gnu" \ CXX="zig c++ -target aarch64-linux-gnu" \ go build -o bin/myapp-linux-arm64 ./cmd/serverZig acts as a self-contained cross-compiler — no separate gcc-aarch64 package needed.
-
Adopt
goreleaserfor the release pipeline..goreleaser.yaml:project_name: myapp builds: - id: myapp main: ./cmd/server env: - CGO_ENABLED=0 flags: - -trimpath ldflags: - -s -w -X main.version={{ .Version }} -X main.commit={{ .Commit }} goos: [linux, darwin] goarch: [amd64, arm64] archives: - format: tar.gz name_template: '{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}' checksum: name_template: 'checksums.txt'Then
goreleaser release --cleanbuilds, archives, checksums, and (with credentials) publishes to GitHub Releases. -
For verification on the destination, re-check
fileoutput and a sanity-version flag:scp bin/myapp-linux-arm64 graviton-host:/usr/local/bin/myapp ssh graviton-host '/usr/local/bin/myapp --version'
Operational notes
GOARMmatters for 32-bit ARM (Raspberry Pi 3 and older).GOARM=7(hardware float) is the modern default;GOARM=5for older boards without FPU. Set explicitly when targeting ARMv6/v7 to avoid the wrong default.- A static binary on macOS (
darwin/arm64) still uses the macOS libSystem dynamically — Go does not produce truly static darwin binaries. That is fine for distribution but be aware. - Embedded
go.sumand module hashes meango buildagainst unverified caches can fail in air-gapped CI.GOFLAGS=-mod=modand a vendoredvendor/directory side-step the network entirely. - For multi-arch Docker images, the canonical pattern is
docker buildx build --platform linux/amd64,linux/arm64with a multi-stage Dockerfile that builds inside the target arch’s base image. Cross-compile in the build stage to dodge QEMU emulation overhead. - Reproducibility check: build twice from a clean tree,
sha256sumthe binaries — they should match. If they do not, something in the build is non-deterministic (timestamps in embedded resources, random map iteration order in generated code).
For the run-side counterpart, see Installing Go and systemd services. Stack Harbor’s managed operations practice runs Go binaries on a mix of amd64 EC2 and ARM64 Graviton — the same release artifact runs on both, the savings on Graviton are real, and the pipeline does not care.