Sling Academy
Home/Golang/Cross-Compiling Go Programs for Any OS

Cross-Compiling Go Programs for Any OS

Last updated: November 27, 2024

Introduction to Cross-Compiling in Go

Go programming language offers a powerful toolset that allows developers to build software for a different target operating system than the one you're currently using. This is called cross-compiling. It is incredibly useful for creating software that can run on different platforms without requiring each platform to have a Go compiler installed.

Setting Up Your Environment

Before you start, you need to have Go installed on your machine. Make sure you have the latest version by visiting Go's official website.

Once installed, you can find your Go version with the following terminal command:

go version

Basic Cross-Compiling Commands

The key to cross-compiling is setting the GOOS (Operating System target) and GOARCH (Processor architecture target) environment variables before compiling your Go application. Here is a simple example:

1. Compile for Windows from Linux/MacOS:


GOOS=windows GOARCH=amd64 go build -o hello.exe hello.go

This command specifies the target OS (windows) and the architecture (amd64), generating a Windows executable from hello.go.

2. Compile for Linux from Windows:


set GOOS=linux
set GOARCH=amd64
go build -o hello hello.go

Note the use of set in Windows PowerShell or Command Prompt to set environment variables.

3. Compile for MacOS from Linux/Windows:


GOOS=darwin GOARCH=amd64 go build -o hello

Checking Supported OS and Architectures

You can view all the supported combinations of GOOS and GOARCH using:


go tool dist list

This will provide all available platforms your Go version can target.

Advanced Cross-Compiling Options

Sometimes you might deal with more complicated setups where additional C libraries are needed on the target OS. For such cases, consider using cgo, but note that this might require setting up a cross-compiler that matches your target architecture and OS.

To enable cgo, set the environment variable CGO_ENABLED=1, but remember it adds complexity as you need the right C cross-compilation tools.

Conclusion

Cross-compiling Go programs can significantly streamline your development process and extend the reach of your applications across different operating systems. The Go toolchain makes it straightforward, empowering developers to create versatile, portable applications.

Next Article: Creating Minimal Docker Images for Go Apps

Previous Article: Compiling and Building Go Binaries

Series: Development and Debugging in Go

Golang

Related Articles

You May Also Like

  • How to remove HTML tags in a string in Go
  • How to remove special characters in a string in Go
  • How to remove consecutive whitespace in a string in Go
  • How to count words and characters in a string in Go
  • Relative imports in Go: Tutorial & Examples
  • How to run Python code with Go
  • How to generate slug from title in Go
  • How to create an XML sitemap in Go
  • How to redirect in Go (301, 302, etc)
  • Using Go with MongoDB: CRUD example
  • Auto deploy Go apps with CI/ CD and GitHub Actions
  • Fixing Go error: method redeclared with different receiver type
  • Fixing Go error: copy argument must have slice type
  • Fixing Go error: attempted to use nil slice
  • Fixing Go error: assignment to constant variable
  • Fixing Go error: cannot compare X (type Y) with Z (type W)
  • Fixing Go error: method has pointer receiver, not called with pointer
  • Fixing Go error: assignment mismatch: X variables but Y values
  • Fixing Go error: array index must be non-negative integer constant