Compile a static, optimized Go binary with embedded version info using ldflags and CGO disabled.
Build a production-ready Go binary by compiling with static linking, disabling CGO, and embedding version information.
go build -ldflags="-s -w -X main.Version=1.0.0" -o myapp main.go
This command creates a statically linked binary named myapp with stripped debug symbols and a version variable set to 1.0.0.
Building a production-ready Go binary creates a single, self-contained program file that runs on any compatible computer without needing extra libraries installed. Think of it like packing a suitcase with everything you need for a trip so you don't have to buy anything at your destination. You use this when deploying software to servers or sharing it with users to ensure it works exactly as expected.