Appendix G. Cross-Compiling Go Programs

 

Go shines in developing cross-platform programs.

It makes it effortless to build for multiple platforms without changing the program's source code. We can write code once and build it for major operating systems.

We only need to set these environment variables when building:

  • GOOS: Sets the target operating system.
  • GOARCH: Sets the target machine's architecture.

They are set to the current machine's configuration by default, as shown below.

$ go env GOOS GOARCH
darwin arm64  #A

Let's list all the operating systems and architectures we can compile our programs for.

$ go tool dist list
aix/ppc64        freebsd/riscv64  linux/mips64le  openbsd/arm
android/386      illumos/amd64    linux/mipsle    openbsd/arm64
android/amd64    ios/amd64        linux/ppc64     openbsd/ppc64
android/arm      ios/arm64        linux/ppc64le   plan9/386
android/arm64    js/wasm          linux/riscv64   plan9/amd64
darwin/amd64     linux/386        linux/s390x     plan9/arm
darwin/arm64     linux/amd64      netbsd/386      solaris/amd64
dragonfly/amd64  linux/arm        netbsd/amd64    wasip1/wasm
freebsd/386      linux/arm64      netbsd/arm      windows/386
freebsd/amd64    linux/loong64    netbsd/arm64    windows/amd64
freebsd/arm      linux/mips       openbsd/386     windows/arm
freebsd/arm64    linux/mips64     openbsd/amd64   windows/arm64