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 [-arch="all"] [-os="all"] [-parallel=] [-ldflags="..."] [-output="{{.Dir}}/{{.OS}}_{{.Arch}}/{{.Base}}"] [packages]

PARAMETERS

-arch
    Specifies the target architectures. Defaults to "all" (all supported architectures).

-os
    Specifies the target operating systems. Defaults to "all" (all supported operating systems).

-parallel
    Sets the number of parallel builds. Defaults to the number of CPUs.

-ldflags
    Passes flags to the Go linker (ld).

-output
    Defines the output path for the compiled binaries. Uses Go templates to customize the output directory and filename.

packages
    The Go packages to build. If not specified, the current directory is used.

DESCRIPTION

gox is a command-line tool designed to simplify cross-compiling Go programs. Instead of manually setting GOOS and GOARCH environment variables and rebuilding for each target platform, gox automates this process. It allows you to build your Go application for multiple operating systems (like Linux, Windows, macOS) and architectures (like amd64, 386, ARM) in parallel.

gox handles the creation of output directories, building the binaries, and optionally archiving them (e.g., creating zip files). It uses Go's built-in cross-compilation capabilities and provides a more convenient and streamlined workflow. This tool significantly reduces the time and effort required to distribute Go applications across different platforms, making it an indispensable tool for Go developers targeting a broad audience. It's especially useful for CI/CD pipelines where automated builds for multiple platforms are essential. gox can also parallelize builds, greatly reducing build times.

CAVEATS

gox relies on Go's built-in cross-compilation support. Ensure that your Go code is compatible with the target operating systems and architectures. Some C dependencies might require platform-specific compilation.

OUTPUT TEMPLATES

The -output flag uses Go templates to define the output path. Available variables include: {{.Dir}} (the directory of the source package), {{.OS}} (the operating system), {{.Arch}} (the architecture), {{.Base}} (the base name of the binary), and {{.Ext}} (the file extension).

DEPENDENCIES

gox requires git installed in the system. When creating builds for multiple architectures, it will try to download and use the corresponding version of the go compiler.
It is highly recommended to use go modules for managing dependencies.

HISTORY

gox emerged as a solution to the challenges of cross-compiling Go applications. Before gox, developers had to manually set environment variables and manage builds for each target platform individually, a tedious and error-prone process. gox automated this workflow, significantly improving the developer experience and reducing the time required to create multi-platform builds. It gained popularity within the Go community due to its simplicity and effectiveness.

SEE ALSO

go(1)

Copied to clipboard