Page Content

Tutorials

Effortless Cross Compilation In Go With GOOS And GOARCH

Cross Compilation in Go

In Go, cross compilation is the process of creating an executable binary file for an operating system or architecture that is different from the one you are working on with Go. One benefit of this strong Go toolchain feature is that it does away with the requirement for distinct build machines for every target platform. In contrast to other languages, Go binaries are self-contained, which means they incorporate all required support code and don’t require Go tools or system dependencies to operate on the target system.

How Go Supports Cross-Compilation

Building for many platforms is much easier with Go since support is built right into the build tool. Essentially, two environment variables are used to do this:

GOOS (Go Operating System): This variable specifies which operating system the executable will be compiled for.

GOARCH (Go Architecture): The compilation architecture for the target system is specified by this variable.

To find the GOOS and GOARCH values of your current system, type go env GOOS GOARCH into your terminal. Darwin and AMD64 would be the outputs, respectively, on a macOS computer with an AMD64 architecture.

Numerous combinations of GOOS and GOARCH are supported by Go. Windows, Linux, Android, Darwin (for macOS), FreeBSD, and Solaris are a few examples of legitimate GOOS values. The corresponding GOARCH values are mips, ppc64, arm, arm64, amd64, and 386. Note that not every pair of GOOS and GOARCH is legitimate, though.

Cross-Compilation Process with an Example

Before using the go build command, you must set the GOOS and GOARCH environment variables to the target you want to use in order to cross-compile a Go application.

Examine the Go application xCompile.go, which outputs details about the Go compiler, architecture, and version:

package main
import (
    "fmt"
    "runtime"
)
func main() {
    fmt.Print("You are using ", runtime.Compiler, " ")
    fmt.Println("on a", runtime.GOARCH, "machine")
    fmt.Println("with Go version", runtime.Version())
}

Output

You are using gc on a amd64 machine
with Go version go1.23.10

It’s crucial to remember that the Go version displayed in the output is the one that was used to compile the binary, not the one that is installed on the target computer where the binary is run.

Platform-Dependent Code with Build Tags and Filename Suffixes

In your Go project, cross-compilation also includes platform-specific code management. For this, Go provides two main mechanisms:

Build Tags: GOOS and GOARCH combinations should be used to compile a certain file. For instance, using // +build linux would guarantee that the file is only compiled for Linux, whereas using // +build!windows would ensure that the file is compiled for all platforms except Windows. Boolean logic can also be combined with these tags (e.g., // +build pro enterprise for pro OR enterprise or // +build pro,enterprise for pro AND enterprise).

Filename Suffixes: The Go standard library frequently adds _GOOS.go or _GOOS_GOARCH.go to a filename as part of its naming convention. For example, path_windows.go does not require an explicit build tag because it tells the Go compiler that the file is meant for the Windows operating system. Code is made simpler by this practice, which divides various platform implementations into separate, neatly organised files.

These characteristics make it possible for programmers to create and manage a single codebase that can be effectively generated into binaries that are compatible with many operating systems and architectures, increasing the program’s reach and streamlining distribution.

You can also read Network Programming In GoLang: TCP, UDP And HTTP

Agarapu Geetha
Agarapu Geetha
My name is Agarapu Geetha, a B.Com graduate with a strong passion for technology and innovation. I work as a content writer at Govindhtech, where I dedicate myself to exploring and publishing the latest updates in the world of tech.
Index