How to Build Static Binaries in Go

Cli
Build a static Go binary by setting CGO_ENABLED=0 or using specific ldflags to link without external dependencies.

Build a static binary by disabling CGO and linking statically with the go build command.

go build -ldflags='-linkmode external -extldflags "-static"' -tags netgo -o myapp main.go

Alternatively, for a simpler static build that works on most Linux systems without external linking:

CGO_ENABLED=0 go build -o myapp main.go