All possible GOOS value?

Learn all possible goos value? with practical examples, diagrams, and best practices. Covers go, cross-compiling development techniques with visual explanations.

Understanding GOOS: A Comprehensive Guide to Go Operating System Values

Hero image for All possible GOOS value?

Explore the full spectrum of GOOS values, their significance in Go's cross-compilation capabilities, and how to effectively manage them for diverse target environments.

Go's powerful cross-compilation features are a cornerstone of its appeal, allowing developers to build executables for various operating systems from a single machine. At the heart of this capability lies the GOOS environment variable, which specifies the target operating system for your Go build. Understanding the available GOOS values is crucial for anyone leveraging Go's cross-platform strengths. This article will delve into the common and less common GOOS values, explain their purpose, and guide you on how to use them effectively.

What is GOOS and Why is it Important?

The GOOS environment variable, short for 'Go Operating System', tells the Go toolchain which operating system the compiled binary is intended to run on. When you compile a Go program, the Go compiler uses GOOS (along with GOARCH for architecture) to select the correct system libraries, syscalls, and other platform-specific code. This enables you to compile a Windows executable on a Linux machine, or a macOS binary on a Windows machine, without needing to set up a virtual machine or a separate build environment for each target OS.

flowchart TD
    A[Go Source Code] --> B{"GOOS & GOARCH Set?"}
    B -- Yes --> C[Go Compiler]
    B -- No --> D[Default GOOS/GOARCH]
    D --> C
    C --> E{"Platform-Specific Libraries"}
    E --> F[Target OS Binary]
    F --> G[Execution on Target OS]

Flowchart illustrating the Go cross-compilation process with GOOS and GOARCH.

Common GOOS Values and Their Usage

While Go supports a wide array of operating systems, a few GOOS values are used most frequently for desktop, server, and common embedded systems. These are the values you'll encounter in most development scenarios. Setting GOOS is typically done via environment variables before running go build or go install.

# Compile for Linux (64-bit x86 architecture)
GOOS=linux GOARCH=amd64 go build -o myapp_linux

# Compile for Windows (64-bit x86 architecture)
GOOS=windows GOARCH=amd64 go build -o myapp_windows.exe

# Compile for macOS (64-bit x86 architecture)
GOOS=darwin GOARCH=amd64 go build -o myapp_macos

Examples of setting GOOS and GOARCH for common target operating systems.

Comprehensive List of Supported GOOS Values

Go's support extends beyond the common desktop and server operating systems to various embedded and less common platforms. The full list of supported GOOS values can be found in the Go source code or by using the go tool dist list command. This command provides a comprehensive list of all supported GOOS/GOARCH combinations.

go tool dist list

Command to list all supported GOOS/GOARCH combinations.

Here's a curated list of notable GOOS values you might encounter:

  • aix: IBM AIX
  • android: Google Android
  • darwin: Apple macOS (formerly OS X)
  • dragonfly: Dragonfly BSD
  • freebsd: FreeBSD
  • illumos: illumos
  • ios: Apple iOS
  • js: JavaScript (for WebAssembly targets)
  • linux: Linux
  • netbsd: NetBSD
  • openbsd: OpenBSD
  • plan9: Plan 9 from Bell Labs
  • solaris: Oracle Solaris
  • windows: Microsoft Windows

Each of these values, when combined with an appropriate GOARCH, allows you to tailor your Go application for a specific execution environment.