Go ships with everything needed to build and deploy an application: the go build, go install, go test and go vet commands, built-in cross-compilation through GOOS and GOARCH, and the embed package for bundling static files into a single binary. Because Go produces a statically linked executable, deployment usually means copying one file or placing it in a minimal container image.
This guide covers the standard toolchain and the library features that matter at build and deploy time.
go buildgo installembedgo buildgo build compiles the package in the current directory and writes an executable. Name the output with -o.
Build a package path (./cmd/myapp) rather than a single file, so all files of the package and your module dependencies are included. Useful flags for release builds:
| Flag | Effect |
|---|---|
-o path | Output file name |
-trimpath | Remove local file system paths from the binary for reproducible builds |
-ldflags "-s -w" | Strip the symbol table and DWARF debug info to shrink the binary |
-ldflags "-X main.version=1.4.0" | Set a string variable at link time |
-race | Build with the race detector (testing only, slower) |
-tags name | Enable build tags |
Injecting a version at link time works like this:
Note that the -X flag only sets package-level string variables. Since Go 1.18 the binary also records module and VCS information, which you can read with go version -m bin/myapp or the runtime/debug package.
Go cross-compiles by setting two environment variables. No extra toolchain is needed as long as the program does not use cgo.
Run go tool dist list to see every supported GOOS/GOARCH pair. Setting CGO_ENABLED=0 forces a fully static binary, which matters when you deploy to a minimal image such as scratch or a distroless base.
go installgo install builds a package and places the binary in $GOBIN, or in $GOPATH/bin when GOBIN is not set (the default GOPATH is $HOME/go).
Since Go 1.16, go install pkg@version is the supported way to install command-line tools. The old go get form for installing binaries no longer builds and installs them. Add $(go env GOPATH)/bin to your PATH to run what you install.
Run the same checks locally and in CI before every build.
Here is a small test that go test picks up automatically because the file name ends in _test.go:
go test also runs benchmarks (-bench) and fuzz tests (-fuzz), so one command covers correctness and performance checks.
embedThe embed package (Go 1.16+) compiles files into the binary, which removes the need to ship templates or static files next to the executable.
The //go:embed static directive embeds the static directory. http.FileServerFS was added in Go 1.22. On older versions use http.FileServer(http.FS(sub)).
A multi-stage Dockerfile compiles in a full Go image and copies only the binary into a small runtime image.
Pin the builder image to the Go version in your go.mod and copy go.mod and go.sum first, so the dependency layer is cached between builds. For services, handle SIGTERM with signal.NotifyContext and call http.Server.Shutdown so deployments drain connections cleanly.
go build and go install?go build writes the executable to the current directory or the path from -o. go install writes it to $GOBIN or $GOPATH/bin so it is available on your PATH.
Set GOOS=linux and GOARCH=amd64 or arm64 before go build. Add CGO_ENABLED=0 if the program does not need cgo.
Go links the runtime and all dependencies statically. Use -ldflags "-s -w" to strip debug data and -trimpath for clean builds, and check imports for heavy packages.
No. The compiled binary has no runtime dependency on Go. A statically linked build runs on a bare Linux host or a minimal container.
Build with go build -trimpath -ldflags "-s -w", cross-compile with GOOS and GOARCH, install tools with go install pkg@version, and verify with go vet, go test -race and go mod verify. Embed assets with embed and ship the static binary in a multi-stage container image. That workflow needs nothing beyond the standard Go toolchain.