Overview

Appendix G. Cross-Compiling Go Programs

Go makes cross-platform development straightforward by allowing you to build binaries for many operating systems and CPU architectures without changing your source code. Cross-compilation is controlled by two environment variables—GOOS (target OS) and GOARCH (target architecture)—which default to your current machine’s settings. You can inspect supported targets with Go’s tooling and quickly see what combinations are available.

To produce a binary for a different platform, set GOOS and GOARCH at build time. For example, you can compile a project for Linux on amd64 and then compile the same code for Windows on amd64, where the resulting executable uses the .exe extension. You can either specify an output filename or let Go infer one (on Windows targets, it automatically adds .exe) based on the current directory name.

Cross-compiled binaries need to be run on their intended OS/architecture; they won’t execute on the machine that built them unless the targets match. After copying the artifacts to their respective platforms, they run directly, and because they are self-contained, end users don’t need Go installed. This approach makes distributing and sharing cross-platform binaries simple and reliable.

FAQ

What makes cross-compiling easy in Go?Go lets you write code once and build for many platforms without changing the source. You only set GOOS (target OS) and GOARCH (target CPU architecture) when running go build.
What are GOOS and GOARCH?GOOS sets the target operating system and GOARCH sets the target CPU architecture. By default, they match your current machine. Check them with:
go env GOOS GOARCH
How do I list all OS/architecture combinations Go can build for?Run:
go tool dist list
It prints supported pairs like linux/amd64, windows/arm64, darwin/arm64, and more.
How do I cross-compile the HIT tool for Linux on AMD64?From the hit/cmd/hit directory, run:
GOOS=linux GOARCH=amd64 go build -o hit-linux-amd64
This produces a hit-linux-amd64 executable in the current directory.
How do I cross-compile the HIT tool for Windows on AMD64?Run:
GOOS=windows GOARCH=amd64 go build -o hit-windows-amd64.exe
Note the .exe extension required for Windows executables.
Can Go add the .exe extension automatically for Windows builds?Yes. If you omit -o and build in the package directory, Go names the binary after the directory and adds .exe automatically:
GOOS=windows GOARCH=amd64 go build
This creates hit.exe in the current directory.
Can I run a cross-compiled binary on my current machine?No. Cross-compiled binaries must be run on the target operating system and architecture. To run on your own machine, don’t set GOOS and GOARCH.
Do users need Go installed to run my cross-compiled binaries?No. The produced executables are self-contained; users can run them without installing Go.
Where does go build write the output, and how do I choose the name?By default, go build writes the executable to the current directory (named after the directory or module). Use -o to set a custom filename, for example:
-o hit-linux-amd64
How do I run the generated binaries on their target platforms?On Linux/AMD64:
./hit-linux-amd64
On Windows/AMD64:
hit-windows-amd64.exe
Copy the binaries to their respective platforms before running.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Go by Example ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Go by Example ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Go by Example ebook for free