Go CLI Cross Compile and Release

Cross-compilation, build flags, ldflags, reproducible releases, CGO controls, and packaging patterns with the go command.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

Cross Compilation

Cross Compilation commands and patterns for the Go command.

List target platforms

Print supported GOOS/GOARCH pairs.

bashANYgocross-compile
bash
go tool dist list
Notes

Print supported GOOS/GOARCH pairs. Useful in Go CLI workflows.

Build Linux amd64

Cross-compile for Linux amd64.

bashANYgocross-compile
bash
GOOS=linux GOARCH=amd64 go build -o dist/app-linux-amd64 .
Notes

Cross-compile for Linux amd64. Useful in Go CLI workflows.

Build macOS arm64

Cross-compile for macOS Apple Silicon.

bashANYgocross-compile
bash
GOOS=darwin GOARCH=arm64 go build -o dist/app-darwin-arm64 .
Notes

Cross-compile for macOS Apple Silicon. Useful in Go CLI workflows.

Build Windows exe

Cross-compile a Windows executable.

bashANYgocross-compile
bash
GOOS=windows GOARCH=amd64 go build -o dist/app.exe .
Notes

Cross-compile a Windows executable. Useful in Go CLI workflows.

Disable CGO

Build without CGO for simpler static-ish builds.

bashANYgocgo
bash
CGO_ENABLED=0 go build .
Notes

Build without CGO for simpler static-ish builds. Useful in Go CLI workflows.

Build Flags

Build Flags commands and patterns for the Go command.

Trim source paths

Remove local filesystem paths from compiled output.

bashANYgobuildreproducible
bash
go build -trimpath .
Notes

Remove local filesystem paths from compiled output. Useful in Go CLI workflows.

Inject version info

Set variables at link time with -ldflags -X.

bashANYgoldflagsversion
bash
go build -ldflags="-X main.version=1.2.3" .
Notes

Set variables at link time with -ldflags -X. Useful in Go CLI workflows.

Use build tags

Compile with specific build tags enabled.

bashANYgobuildtags
bash
go build -tags "prod netgo" .
Notes

Compile with specific build tags enabled. Useful in Go CLI workflows.

Require clean mod files

Disallow automatic go.mod/go.sum edits during build.

bashANYgobuildmodules
bash
go build -mod=readonly ./...
Notes

Disallow automatic go.mod/go.sum edits during build. Useful in Go CLI workflows.

Emit build JSON

Use JSON output to integrate with tools.

bashANYgobuildjson
bash
go build -json ./...
Notes

Use JSON output to integrate with tools. Useful in Go CLI workflows.

Release Packaging

Release Packaging commands and patterns for the Go command.

Test then build

A typical release pipeline pattern.

bashANYgorelease
bash
go test ./... && go build -trimpath -o ./dist/app .
Notes

A typical release pipeline pattern. Useful in Go CLI workflows.

Generate code before build

Run go generate directives before compiling.

bashANYgogeneraterelease
bash
go generate ./... && go build ./...
Notes

Run go generate directives before compiling. Useful in Go CLI workflows.

Build command packages

Install all command packages under cmd/.

bashANYgoinstallrelease
bash
go install ./cmd/...
Notes

Install all command packages under cmd/. Useful in Go CLI workflows.

Inspection and Binary Introspection

Inspection and Binary Introspection commands and patterns for the Go command.

Inspect build info

Show module/build info embedded in a binary.

bashANYgobinaryinspection
bash
go version -m ./dist/app
Notes

Show module/build info embedded in a binary. Useful in Go CLI workflows.

List binary symbols

Inspect symbols in a compiled binary.

bashANYgotoolbinary
bash
go tool nm ./dist/app
Notes

Inspect symbols in a compiled binary. Useful in Go CLI workflows.

Disassemble binary

Disassemble functions from a compiled binary.

bashANYgotoolbinary
bash
go tool objdump ./dist/app
Notes

Disassemble functions from a compiled binary. Useful in Go CLI workflows.

Recommended next

No recommendations yet.