Go cross compilation lets you build a program for Windows, macOS or Linux from one computer, with no extra tools. In this post, you will build for different systems and shrink your binaries. Then you will stamp a version number and write code for one system only. Also, each step comes with commands you can run.
Earlier, you first ran go build in install Go and run your first program. We will also ship the kind of tool from the Go flag package lesson. After all, people run command line tools on many systems.
What Is Go Cross Compilation?
Normally, a compiler builds a program for the machine it runs on. In contrast, cross compiling means building for a different machine. For example, you can build a Windows .exe on a Linux laptop.
Still, most languages make this hard. Go, however, ships every supported target inside the normal toolchain. So you just set two environment variables, as you learned in Go environment variables for configuration.
GOOS and GOARCH: The Heart of Go Cross Compilation
GOOS picks the operating system, and GOARCH picks the processor type. For example, the diagram below shows one source folder turning into three different binaries.

One go build command with different GOOS and GOARCH values produces native binaries for Windows, macOS and Linux.
$ GOOS=windows GOARCH=amd64 go build -o hello.exe .
$ file hello.exe
hello.exe: PE32+ executable (console) x86-64, for MS Windows
$ GOOS=darwin GOARCH=arm64 go build -o hello-mac .
$ file hello-mac
hello-mac: Mach-O 64-bit arm64 executable
$ GOOS=linux GOARCH=arm64 go build -o hello-arm .
Every command ran on the same Linux machine. Yet each file is a real native program for its target. In short, that is Go cross compilation in action. On Windows PowerShell, set the variables first with $env:GOOS="linux" and then run go build.
Common Targets
# target GOOS GOARCH
Windows PC windows amd64
Apple Silicon Mac darwin arm64
Intel Mac darwin amd64
Linux server linux amd64
Raspberry Pi 4 or AWS Graviton linux arm64
Also, run go tool dist list to see every pair. Go 1.24 lists 48 of them, and go env GOOS GOARCH shows your own machine.
Go Cross Compilation and cgo
$ CGO_ENABLED=0 go build -o hello-static .
$ file hello-static
hello-static: ELF 64-bit LSB executable, x86-64, statically linked
For pure Go code, Go cross compilation works with no extra effort. However, a package that uses cgo, Go’s bridge to C code, needs a C compiler for the target. So set CGO_ENABLED=0 when you can. As a result, you get one static file with no outside dependencies, which suits tiny containers.
Smaller Binaries and Version Numbers
$ go build -o hello .
$ go build -ldflags="-s -w" -o hello-small .
$ ls -l hello hello-small
2213620 hello
1437880 hello-small
First, the -s -w linker flags drop debug information. In fact, that cut the file by about 35 percent in this test. Keep the full build for debugging, though, because stack traces need that data.
var version = "dev"
$ go build -ldflags="-X main.version=1.2.0" -o hello .
$ ./hello
version: 1.2.0
Similarly, the -X flag sets a string variable at build time. As a result, every release binary knows its own version without editing code.
Code for One System Only
// path_windows.go
func configDir() string { return `C:\ProgramData\hello` }
// path_other.go
//go:build !windows
func configDir() string { return "/etc/hello" }
For example, a file ending in _windows.go only builds for Windows. Meanwhile, a //go:build line at the top gives finer control. In addition, runtime.GOOS tells running code which system it is on.
Common Go Cross Compilation Mistakes
| Error you see | Why it happens | How to fix it |
|---|---|---|
| go: unsupported GOOS/GOARCH pair plan10/amd64 | Because the target name is misspelled. | So check go tool dist list. |
| exec format error | Because the binary was built for another system. | So match GOOS and GOARCH to the server. |
| cgo build fails for the target | Because a package needs a C compiler. | So set CGO_ENABLED=0 or use a pure Go package. |
| macOS warns the app is from an unknown developer | Because the binary is not signed. | So sign it, or allow it in System Settings. |
Build and release questions also appear in DevOps interviews. So practise with my Go interview questions and answers.
Official Resources
These pages come straight from the Go team, so they stay accurate and up to date.
- Go docs: Compile and install the application
- cmd/go: Build constraints
- Go docs: Optional environment variables (GOOS and GOARCH)
Quick Recap
- Go cross compilation needs only the GOOS and GOARCH variables.
go tool dist listshows every supported target.CGO_ENABLED=0gives simple, static binaries.-ldflags="-s -w"shrinks files, and-Xstamps a version.- File suffixes and
//go:buildlines add system-specific code.
What’s Next?
You can now ship one codebase to every major system. Next, we will package a Go web app into a small container image with Docker.
Next lesson: Go Docker Deployment: Build Tiny Container Images


