Skip to content

Instantly share code, notes, and snippets.

@zfarbp
Last active May 28, 2024 05:09
Show Gist options
  • Save zfarbp/121a76d5a3fde562c3955a606a9d6fcc to your computer and use it in GitHub Desktop.
Save zfarbp/121a76d5a3fde562c3955a606a9d6fcc to your computer and use it in GitHub Desktop.
Golang - Building Executables for Different Architectures

Golang - Building Executables for Different Architectures

env GOOS=target-OS GOARCH=target-architecture go build package-import-path

# Example
env GOOS=darwin GOARCH=amd64 go build
env GOOS=darwin GOARCH=amd64 go build main.go
env GOOS=darwin GOARCH=amd64 go build github.com/zoo/york/foo/bar

# Raspberry pi
env GOOS=linux GOARCH=arm GOARM=5 go build
GOOS - Target Operating System GOARCH - Target Platform
android arm
darwin 386
darwin amd64
darwin arm
darwin arm64
dragonfly amd64
freebsd 386
freebsd amd64
freebsd arm
linux 386
linux amd64
linux arm
linux arm64
linux ppc64
linux ppc64le
linux mips
linux mipsle
linux mips64
linux mips64le
netbsd 386
netbsd amd64
netbsd arm
openbsd 386
openbsd amd64
openbsd arm
plan9 386
plan9 amd64
solaris amd64
windows 386
windows amd64
@Sia200
Copy link

Sia200 commented Jul 8, 2021

Thanks

@alekssamos
Copy link

Can you explain in detail how this is possible? And why can't this be done in other languages? In other languages, the assembly for each system must be compiled within that system, but not on other systems. Simply put, how did the Compiler achieve cross-platform functionality?

@zfarbp
Copy link
Author

zfarbp commented Feb 9, 2024

Go achieves cross-compilation primarily through its compiler and standard library design, enabling developers to easily build executables for different operating systems and architectures from a single source code. The key aspects that facilitate this capability in Go are:

  1. Platform-Agnostic Standard Library: Go's standard library is designed to work across different platforms, using conditional compilation to select the appropriate implementation for the target OS and architecture.

  2. Compiler Design: The Go compiler supports generating machine code for various target platforms directly from any supported host platform. This is achieved by abstracting target-specific details and including support for multiple architectures within the compiler.

  3. Environment Variables: Go uses environment variables (GOOS for the operating system and GOARCH for the architecture) to specify the compilation target, making it straightforward to cross-compile without modifying the source code.

  4. Simplified Dependency Management: Go's module system simplifies managing dependencies for different platforms, aiding in the cross-compilation process.

The reason this isn't as straightforward in many other programming languages is due to factors like the lack of a unified compiler capable of targeting multiple platforms, reliance on platform-specific libraries or system calls that require different code paths for different operating systems and more complex dependency management when dealing with cross-platform libraries. Additionally, the toolchain and build processes in other languages might not be as streamlined for cross-compilation, requiring additional tools or configurations to achieve similar outcomes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment