Programs written in Go can easily be compiled for a wide variety of target operating systems such as Windows, macOS, and Linux by using the
GOOS environmental variables. These represent the compilation architecture and the name of the target operating system respectively, and are set to your host compilation architecture (
GOHOSTARCH) and operating system (
GOHOSTOS) by default.
You can use the
go env command to view the values of these variables on your machine:
Compile Go project for Windows
Here’s the command you need to run to compile your Go project for a 64-bit Windows machine:
In this scenario,
GOOS is windows, and
amd64 indicating a 64-bit architecture. If you need to support a 32-bit architecture, all you need to do is change
Compile Go project for macOS
GOARCH values for Windows are also valid for macOS, but in this case the required
GOOS value is
Compile Go project for Linux
To build your Go program for Linux, use
linux as the value of
GOOS and the appropriate
GOARCH value for your target CPU architecture:
Other operating systems and CPU architectures
Go supports a lot more OS and CPU architecture combinations than what I’ve described above, but these are the most used ones. If you need to compile for ARM, Android, Web Assembly, or other targets, please refer to the Go docs to see the combinations of
GOARCH that are available to you.
Thanks for reading, and happy coding!