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
A static binary includes all the code it needs to run, so you don't have to install extra libraries on the computer where you run it. Think of it like a self-contained lunchbox versus a meal that requires you to visit a restaurant for the ingredients. You use this when you want to ship your program to a server or container without worrying about missing dependencies.