Go programs are usually deployed as a single statically linked binary, copied to a server or packed into a minimal container image. You choose the target with GOOS and GOARCH, build with go build, and distribute the result through a container registry, a release archive, a package manager or go install. Because there is no runtime to install, deployment is simpler than for most languages.
This guide covers the techniques that matter in production: static binaries, cross-compilation, Docker multi-stage builds, versioning, release automation and cloud targets.
The Go toolchain compiles all Go dependencies into one executable. Setting CGO_ENABLED=0 also removes the dependency on the C library, so the binary runs on any Linux distribution, including empty scratch images.
-trimpath removes local file paths from the binary so builds are reproducible, and -s -w strips the symbol table and DWARF data to shrink the file. Run go tool dist list to see every supported GOOS/GOARCH pair. If your code uses cgo, for example for SQLite drivers that wrap C, cross-compiling needs a C cross-compiler, so prefer pure Go drivers when you can.
A multi-stage build compiles in a full Go image and copies only the binary into a tiny runtime image.
The final image is often under 20 MB, has no shell and runs as a non-root user, which reduces the attack surface. Copying go.mod and go.sum first lets Docker cache the dependency download layer. Use the same Go version your go.mod expects, and pin base images by digest for reproducible builds. For multi-architecture images, build with docker buildx build --platform linux/amd64,linux/arm64.
Release binaries should report which version they are. Inject values at link time with -ldflags -X:
The variables must be package-level strings. Go also stamps VCS information automatically; read it with go version -m bin/myapp or debug.ReadBuildInfo(). Follow semantic versioning for tags such as v1.4.2, and for libraries use the module rules: a major version of 2 or higher changes the import path to end in /v2.
| Channel | Best for | How |
|---|---|---|
| Container registry | Services on Kubernetes, ECS, Cloud Run | docker push an image per version tag |
| Release archives | CLIs for end users | Attach tar.gz or zip files and checksums to a GitHub release |
go install | Developer tools | go install example.com/tool/cmd/tool@latest |
| OS packages | Servers and desktops | .deb, .rpm or Homebrew formulas |
| Plain copy | Small internal services | scp the binary and restart the service |
GoReleaser automates most of the table: from one config file it cross-compiles, archives, checksums, signs, builds container images and publishes a release when you push a tag.
For a virtual machine, copy the binary and run it under systemd:
Managed platforms are even simpler. Google Cloud Run, AWS App Runner and Azure Container Apps all take a container image and handle scaling, TLS and rollouts. AWS Lambda runs Go on the provided.al2023 custom runtime, with a binary named bootstrap. Whatever the target, follow these habits:
log/slog.SIGTERM with http.Server.Shutdown so rollouts do not drop requests.GOMAXPROCS.Before a release goes out, run go vet, tests with -race and govulncheck ./... in CI. Publish a checksum file, and sign artifacts and images with a tool such as Sigstore cosign so consumers can verify them. Keep the previous version available so you can roll back by redeploying the old tag.
With CGO_ENABLED=0 yes: it needs only the kernel. With cgo enabled, the binary links to the system C library unless you build with static linking flags.
Set GOOS and GOARCH when you run go build, for example GOOS=linux GOARCH=arm64 go build. No extra toolchain is needed for pure Go code.
Both work for static binaries. Distroless adds CA certificates, time zone data and a non-root user, which a bare scratch image lacks, so it is the safer default.
Build with -ldflags="-s -w" and -trimpath, avoid pulling in large dependencies you do not use, and optionally compress with UPX if startup time is not critical.
Build a static binary with CGO_ENABLED=0, cross-compile with GOOS and GOARCH, package it in a multi-stage Docker image and stamp it with a version through -ldflags. Distribute through a registry, release archives or go install, automate with GoReleaser and deploy to a VM, a container platform or a serverless runtime.