Skip to content
Latchkey

How to Cross-Compile Go for Multiple GOOS and GOARCH

Go cross-compiles natively, so setting GOOS and GOARCH environment variables produces a binary for any target from a single Linux runner.

Pure Go needs no emulation to cross-compile. Drive GOOS and GOARCH from a matrix and one Linux runner can emit every platform binary in parallel.

Steps

  • Define a matrix of goos/goarch include entries.
  • Export GOOS and GOARCH before go build.
  • Name the output binary with the OS and arch so artifacts stay distinct.

Workflow

.github/workflows/release.yml
jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        include:
          - { goos: linux,   goarch: amd64 }
          - { goos: linux,   goarch: arm64 }
          - { goos: darwin,  goarch: arm64 }
          - { goos: windows, goarch: amd64 }
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-go@v5
        with:
          go-version: '1.22'
      - env:
          GOOS: ${{ matrix.goos }}
          GOARCH: ${{ matrix.goarch }}
        run: go build -o "dist/app-${{ matrix.goos }}-${{ matrix.goarch }}" ./cmd/app

Gotchas

  • Packages that use cgo need CGO_ENABLED=0 or a matching cross C toolchain, or the build fails to link.
  • Windows targets get an .exe suffix; add it to the output name for GOOS windows.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →