go build compiles a Go program and its dependencies into a single binary, statically linked by default — no runtime, interpreter, or shared library needs to be installed on the machine that runs it. Cross-compiling for a different operating system or CPU architecture requires no separate toolchain: setting the GOOS and GOARCH environment variables before go build is enough, since the Go toolchain ships the pieces needed for every supported target.
A basic build
go build compiles the current package into an executable named after the module (or module directory); -o names it explicitly.
$ go build -o myapp .
$ ./myapp // runs immediately — no separate runtime needed
$ ls -lh myapp // a single, several-MB, dependency-free binaryCross-compiling with GOOS and GOARCH
Setting these two variables before the build targets any supported combination, without installing anything extra — this is a large part of why Go is popular for building CLI tools distributed across many platforms.
$ GOOS=linux GOARCH=amd64 go build -o myapp-linux .
$ GOOS=windows GOARCH=amd64 go build -o myapp.exe .
$ GOOS=darwin GOARCH=arm64 go build -o myapp-mac-m1 .
$ go tool dist list // lists every supported GOOS/GOARCH combinationBuild tags for conditional compilation
A build tag comment at the top of a file (or a filename suffix like _linux.go) restricts that file to specific platforms or conditions, letting a single codebase have OS-specific implementations that the build automatically picks between.
//go:build linux
package config
const ConfigPath = "/etc/myapp/config.yaml"