Go Academy · Lesson

Distributing CLI Tools

Cross-compilation, goreleaser, and homebrew taps

Lesson 4 of 413 steps

Distributing CLI Tools is a free Go Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, and your progress syncs across the web and the CoddyKit app. The Go Academy course includes 4 lessons in total.

go install for local distribution

go install github.com/user/tool@latest compiles and installs the binary into $GOPATH/bin. Users need Go installed.

Cross-compilation

Go cross-compiles natively. Set GOOS and GOARCH to target different operating systems:

GOOS=linux  GOARCH=amd64 go build -o myapp-linux-amd64
GOOS=darwin GOARCH=arm64 go build -o myapp-darwin-arm64
GOOS=windows GOARCH=amd64 go build -o myapp-windows-amd64.exe

Build flags for release

Strip debug info and disable CGO for smaller, static binaries:

CGO_ENABLED=0 go build -ldflags="-s -w" -o myapp

Embedding version info

Inject build-time information with ldflags:

var version = "dev"
var commit  = "none"
// Build: go build -ldflags="-X main.version=1.2.3 -X main.commit=abc1234"

GoReleaser

GoReleaser automates cross-compilation, packaging (tar.gz, .zip), checksum generation, and uploading releases to GitHub. Configure in .goreleaser.yaml.

# .goreleaser.yaml
builds:
  - env: [CGO_ENABLED=0]
    goos: [linux, darwin, windows]
    goarch: [amd64, arm64]

Homebrew tap

Publish a Homebrew formula so macOS/Linux users can install with brew install. GoReleaser can generate the formula automatically.

GitHub Actions CI/CD

Use a GitHub Action to build and release on every tag push. GoReleaser has an official GitHub Action.

- name: Run GoReleaser
  uses: goreleaser/goreleaser-action@v5
  with: { version: latest, args: release --clean }
  env: { GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}" }

Docker distribution

Package the CLI as a Docker image for users who prefer containers. Use a multi-stage build to keep the final image minimal.

FROM golang:1.22 AS build
WORKDIR /app
COPY . .
RUN CGO_ENABLED=0 go build -o myapp

FROM scratch
COPY --from=build /app/myapp /myapp
ENTRYPOINT ["/myapp"]

go:embed for assets

Embed static files (templates, config schemas) into the binary with //go:embed so the binary is fully self-contained.

//go:embed templates/*
var templates embed.FS

Versioned modules

Publish your CLI module following Go module versioning. For v2+, the import path must include /v2 and the go.mod module path must match.

Backward compatibility

Follow semver. Minor releases may add flags but must not remove or rename existing ones. Breaking changes require a major version bump.

Quick Check

How do you build a static Go binary for Linux on a macOS machine?

Recap: Distributing CLI Tools

Key points:

  • Cross-compile with GOOS/GOARCH; CGO_ENABLED=0 for static binaries
  • GoReleaser for automated release pipeline
  • Inject version with -ldflags "-X main.version=..."
  • go:embed for self-contained binaries with embedded assets
Free to start

Learn Go with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

Is the “Distributing CLI Tools” lesson free?

Yes — any 3 lessons of the Go Academy learning path, including “Distributing CLI Tools”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Go Academy course includes 4 lessons in total.

What will I learn in “Distributing CLI Tools”?

Cross-compilation, goreleaser, and homebrew taps You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Go Academy?

No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

How long does the “Distributing CLI Tools” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Go Academy lesson?

Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Cobra Fundamentals
  2. Flags and Persistent Flags
  3. Subcommands and Command Groups
  4. Distributing CLI Tools
← Back to Go Academy