LinuxCommandLibrary

gox

Cross-compile Go programs for multiple platforms

TLDR

Compile Go program in the current directory for all operating systems and architecture combinations

$ gox
copy

Download and compile a Go program from a remote URL
$ gox [url_1 url_2 ...]
copy

Compile current directory for a particular operating system
$ gox -os="[os]"
copy

Compile current directory for a single operating system and architecture combination
$ gox -osarch="[os]/[arch]"
copy

SYNOPSIS

gox [-os=OSLIST] [-arch=ARCHLIST] [-osarch=OSARCHLIST] [-output=FORMAT] [-parallel=N] [-verbose] [PACKAGES...]

PARAMETERS

-arch=ARCHLIST
    Comma-separated architectures, e.g., amd64,arm

-os=OSLIST
    Comma-separated OSes, e.g., linux,darwin,windows

-osarch=OSARCHLIST
    Comma-separated OS/arch pairs, e.g., linux/amd64,darwin/arm64

-output=FORMAT
    Output filename template, e.g., {{.Dir}}/{{.Os}}_{{.Arch}}/{{.Project}}{{.Ext}} (default: {{.Project}}_{{.Os}}_{{.Arch}}{{.Ext}})

-parallel=N
    Max parallel builds (default: 4)

-verbose
    Enable verbose output

-ldflags=FLAGS
    Pass linker flags to go build

-gcflags=FLAGS
    Pass compiler flags to go build

DESCRIPTION

gox is a lightweight command-line tool designed to simplify cross-compiling Go programs across various operating systems and architectures. It automates the process of building binaries for different GOOS and GOARCH combinations, which is essential for developers distributing Go applications without manual environment variable tweaks or repeated builds.

By specifying target OSes, architectures, or OS/arch pairs, gox parallelizes the builds, outputs binaries in a customizable format, and handles dependencies efficiently. It's particularly useful for CI/CD pipelines, release automation, and creating multi-platform releases from a single command. While modern Go supports native cross-compilation via GOOS/GOARCH env vars and go build, gox streamlines multi-target workflows with parallelism and templated outputs.

Installation typically involves go install github.com/mitchellh/gox@latest, requiring a properly set GOPATH or Go modules. It's not a core Linux utility but a Go ecosystem tool runnable on Linux systems with Go installed.

CAVEATS

Requires Go 1.5+ installed; modern Go (1.21+) has improved native cross-compile support, potentially reducing gox necessity. Not all architectures may be available without Go toolchain setup. Outputs to current directory by default.

INSTALLATION

go install github.com/mitchellh/gox@latest
Ensure Go modules or GOPATH configured.

EXAMPLE USAGE

gox -osarch="linux/amd64,darwin/amd64,windows/amd64" -output="dist/{{.Os}}_{{.Arch}}" ./cmd/myapp
Builds myapp for three platforms into dist/.

HISTORY

Originally created by Andrew Bonventre (kevlarlb/gox) in 2012 for simple multi-platform Go builds. Popular fork by Mitchell Hashimoto (mitchellh/gox) added features like parallelism and improved templating. Widely used pre-Go 1.5; usage declined with Go's native cross-compile enhancements but remains handy for automation.

SEE ALSO

go(1), make(1)

Copied to clipboard