Go supports build and deployment through a single toolchain: go build compiles your module into a self-contained, statically linked binary, GOOS and GOARCH cross-compile it for other platforms, and that one file can be copied to a server or placed in a minimal container image. You do not need a runtime, a package manager or a separate build tool on the target machine.
This guide covers the commands you use every day, reproducible and smaller builds, cross-compilation, container images, and a simple CI pipeline.
Every Go program lives in a module, described by a go.mod file. The go command reads it, downloads dependencies, verifies them against go.sum, and compiles the main package into an executable.
| Command | What it does |
|---|---|
go run . | Compiles to a temporary location and runs; use it while developing |
go build -o bin/hello . | Compiles the package in the current directory into bin/hello |
go install ./cmd/hello | Builds and places the binary in $GOBIN (or $GOPATH/bin) |
go install example.com/tool@latest | Downloads, builds and installs a tool at a given version |
go mod tidy | Adds missing and removes unused dependencies in go.mod |
Use go run for quick checks and go build for anything you ship. Since Go 1.16 go install pkg@version is the standard way to install command-line tools; the old habit of using go get for that has been removed.
Cross-compilation needs no extra tooling. Set GOOS and GOARCH for the build:
Run go tool dist list to see every supported pair. If your code or a dependency uses cgo (C code), cross-compiling needs a C cross-compiler. For pure Go programs, set CGO_ENABLED=0 so the result never depends on the system C library:
With CGO_ENABLED=0, Go produces a fully static binary that runs on any Linux distribution of the same architecture, including scratch containers. Without it, packages such as net and os/user may link against the system C library depending on the platform.
Useful flags for release builds:
-trimpath removes local file system paths from the binary, which helps reproducibility.-ldflags="-s -w" strips the symbol table and DWARF debug information, making the file smaller. Stack traces from panics still work, but debuggers lose information.-X main.version=1.4.2 sets a string variable at link time, a common way to embed a version. It must be a package-level string variable, for example var version = "dev".Since Go 1.18 the binary also records module and VCS information, which you can read with go version -m dist/app. Pin the Go version in go.mod with the go and optional toolchain directives; modern Go can download the required toolchain automatically, so CI and developers build with the same compiler.
The most common deployment target is a container. A multi-stage build compiles in a full Go image and copies only the binary into a small final image:
Copying go.mod and go.sum first lets Docker cache the dependency download until they change. A scratch or distroless base keeps the image small and reduces its attack surface. If your app calls HTTPS endpoints, make sure the final image has CA certificates (distroless includes them; with scratch you must copy them in). Use a Go image tag that matches the version in your go.mod.
A pipeline should test, vet and build on every change. A minimal GitHub Actions workflow:
go-version-file: go.mod keeps CI on the version the project declares. Pin action versions to whatever is current in your organisation. The same steps work in GitLab CI, Jenkins or any other system, because they are just go commands.
| Strategy | When it fits |
|---|---|
Copy the binary and restart a systemd unit | One or a few servers, simple services |
| Container on a managed platform (Cloud Run, ECS, Fly.io, App Service) | Most web services; no server management |
| Kubernetes with rolling updates | Many services, autoscaling, strict availability needs |
| Release binaries for users (GoReleaser, GitHub Releases) | Command-line tools and desktop utilities |
For the plain server case, a small script is enough, as long as it fails on the first error and restarts the service only after the copy succeeds:
Whatever you choose, expose a health endpoint, shut down gracefully on SIGTERM with http.Server.Shutdown, and keep the previous binary or image so you can roll back quickly.
No. The compiled binary contains the runtime and all Go dependencies. With CGO_ENABLED=0 it also has no dependency on system libraries.
go build and go install?go build writes the executable to the current directory or the path given with -o. go install builds and places it in $GOBIN (default $GOPATH/bin), which is meant for tools you want on your PATH.
It includes the Go runtime and every package you use. Build with -ldflags="-s -w" to strip debug data and -trimpath for cleaner output. Compressors such as UPX are optional and can cause problems with antivirus tools and startup time.
Declare var version = "dev" in main and build with -ldflags="-X main.version=1.4.2". You can also read build information at runtime through runtime/debug.ReadBuildInfo.
Go builds a deployable artifact with one command. Use go build with -trimpath and -ldflags for release binaries, set GOOS, GOARCH and CGO_ENABLED=0 to cross-compile static files, package them in a multi-stage container build, and automate the same steps in CI. Choose the deployment target by your scale, from a systemd service to a managed container platform or Kubernetes.