How does cross-compilation work in Go?

Answer

Go supports cross-compilation natively by setting two environment variables: GOOS (target operating system: linux, windows, darwin, android, etc.) and GOARCH (target architecture: amd64, arm64, 386, etc.) before running go build. Example: GOOS=linux GOARCH=arm64 go build -o myapp-linux-arm64 ./cmd/myapp. No separate cross-compiler toolchain is needed for pure Go code — the standard library includes implementations for all supported platforms. CGo breaks this: cross-compiling with CGo requires a C cross-compiler for the target. Go supports over 40 OS/architecture combinations, making it one of the best languages for building multi-platform binaries in CI/CD pipelines.