codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐹

Go (Golang)

66 / 72 topics
63CGO (Calling C Code from Go)64Go Plugins65Embedding Languages in Go66Cross-Compilation
Tutorials/Go (Golang)/Cross-Compilation
🐹Go (Golang)

Cross-Compilation

Updated 2026-04-20
4 min read

Cross-Compilation

Introduction

Cross-compilation is a powerful feature that allows you to build binaries for different operating systems and architectures from a single machine. This capability is particularly useful when you want to distribute your Go applications across multiple platforms without needing to set up separate development environments for each one.

Go, being a statically typed language with a strong emphasis on simplicity and efficiency, makes cross-compilation straightforward compared to many other languages. In this guide, we will explore how to perform cross-compilation in Go, understand the underlying concepts, and discuss best practices for leveraging this feature effectively.

Understanding Cross-Compilation

Cross-compilation involves compiling code for a target environment that is different from the one where the compilation is taking place. This typically requires specifying the operating system (GOOS) and architecture (GOARCH) of the target platform.

Key Concepts

  1. GOOS: Stands for "Go Operating System". It specifies the target operating system. Common values include:

    • linux
    • darwin (macOS)
    • windows
    • freebsd
    • openbsd
  2. GOARCH: Stands for "Go Architecture". It specifies the target architecture. Common values include:

    • amd64
    • 386
    • arm
    • arm64
    • ppc64le
  3. CGO_ENABLED: This environment variable controls whether cgo is enabled during compilation. By default, it is set to 1 (enabled). Disabling cgo can be useful for cross-compilation when the target platform does not have a C compiler available.

Setting Up Your Environment

Before you start cross-compiling, ensure that your Go environment is properly configured:

  1. Install Go: Make sure you have Go installed on your machine. You can download it from the official Go website.

  2. Set GOPATH and GOROOT: Ensure that your GOPATH and GOROOT environment variables are set correctly.

  3. Enable Cross-Compilation: By default, Go supports cross-compilation out of the box. However, you may need to install additional tools or libraries for certain target platforms.

Basic Cross-Compilation

Let's start with a simple example to demonstrate how to perform basic cross-compilation in Go.

Example: Building a Binary for Windows on Linux

Suppose you have a simple Go program that prints "Hello, World!" and you want to build it as an executable for Windows on a Linux machine.

// main.go
package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
}

To compile this program for Windows (64-bit), you would use the following command:

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

This command sets the target operating system to windows and the architecture to amd64. The -o flag specifies the output file name.

Explanation

  • GOOS=windows: Sets the target operating system to Windows.
  • GOARCH=amd64: Sets the target architecture to 64-bit AMD.
  • go build: Compiles the Go program.
  • -o hello.exe: Specifies the output file name as hello.exe.

After running this command, you will find a hello.exe file in your current directory, which is executable on Windows.

Advanced Cross-Compilation

Disabling CGO

In some cases, especially when targeting platforms without a C compiler, you may need to disable cgo. This can be done by setting the CGO_ENABLED environment variable to 0.

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

Cross-Compiling for ARM Devices

Cross-compiling for ARM devices is common when targeting embedded systems or Raspberry Pi. Here's how you can compile a Go program for an ARM device:

GOOS=linux GOARCH=arm GOARM=7 go build -o hello_arm main.go

Explanation

  • GOARM: Specifies the ARM version. Common values are 5, 6, and 7.

Best Practices

  1. Test Your Binaries: Always test your cross-compiled binaries on the target platform to ensure they work as expected.

  2. Use Build Tags: Go supports build tags that allow you to include or exclude code based on the build environment. This can be useful for writing platform-specific code.

    // +build windows
    
    package main
    
    import "fmt"
    
    func main() {
        fmt.Println("Hello, Windows!")
    }
    
  3. Automate with Makefiles: For complex projects, consider using Makefiles to automate the cross-compilation process.

  4. Keep Dependencies Up-to-Date: Ensure that all dependencies are compatible with the target platforms and architectures.

  5. Use Docker for Consistency: If you encounter issues with different environments, consider using Docker containers to maintain consistency across builds.

Real-World Example

Let's walk through a more complex example where we build a cross-platform CLI tool using Go.

Step 1: Create a Simple CLI Tool

// main.go
package main

import (
    "flag"
    "fmt"
    "os"
)

func main() {
    name := flag.String("name", "World", "Your name")
    flag.Parse()

    fmt.Printf("Hello, %s!\n", *name)
}

Step 2: Create a Makefile for Cross-Compilation

# Makefile
TARGETS = linux/amd64 windows/amd64 darwin/amd64 freebsd/amd64 openbsd/amd64

all: $(TARGETS)

linux/amd64:
    GOOS=linux GOARCH=amd64 go build -o bin/linux_amd64/cli main.go

windows/amd64:
    GOOS=windows GOARCH=amd64 CGO_ENABLED=0 go build -o bin/windows_amd64/cli.exe main.go

darwin/amd64:
    GOOS=darwin GOARCH=amd64 go build -o bin/darwin_amd64/cli main.go

freebsd/amd64:
    GOOS=freebsd GOARCH=amd64 go build -o bin/freebsd_amd64/cli main.go

openbsd/amd64:
    GOOS=openbsd GOARCH=amd64 go build -o bin/openbsd_amd64/cli main.go

Step 3: Build the Binaries

Run make in your terminal to build binaries for all specified targets:

make

This will create a bin directory with executables for each target platform.

Conclusion

Cross-compilation is a powerful feature that enhances the portability and reach of Go applications. By understanding how to specify target operating systems and architectures, you can easily build binaries for multiple platforms from a single machine. This guide has provided a comprehensive overview of cross-compilation in Go, including basic examples, advanced techniques, best practices, and real-world applications. Whether you're developing software for embedded systems or targeting diverse user bases, mastering cross-compilation will significantly improve your development workflow and application distribution capabilities.


PreviousEmbedding Languages in GoNext Go Modules for Dependency Management

Recommended Gear

Embedding Languages in GoGo Modules for Dependency Management