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
List target platforms
go tool dist list

# Print supported GOOS/GOARCH pairs.

Build Linux amd64
GOOS=linux GOARCH=amd64 go build -o dist/app-linux-amd64 .

# Cross-compile for Linux amd64.

Build macOS arm64
GOOS=darwin GOARCH=arm64 go build -o dist/app-darwin-arm64 .

# Cross-compile for macOS Apple Silicon.

Build Windows exe
GOOS=windows GOARCH=amd64 go build -o dist/app.exe .

# Cross-compile a Windows executable.

Disable CGO
CGO_ENABLED=0 go build .

# Build without CGO for simpler static-ish builds.

## Build Flags
Trim source paths
go build -trimpath .

# Remove local filesystem paths from compiled output.

Inject version info
go build -ldflags="-X main.version=1.2.3" .

# Set variables at link time with -ldflags -X.

Use build tags
go build -tags "prod netgo" .

# Compile with specific build tags enabled.

Require clean mod files
go build -mod=readonly ./...

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

Emit build JSON
go build -json ./...

# Use JSON output to integrate with tools.

## Release Packaging
Test then build
go test ./... && go build -trimpath -o ./dist/app .

# A typical release pipeline pattern.

Generate code before build
go generate ./... && go build ./...

# Run go generate directives before compiling.

Build command packages
go install ./cmd/...

# Install all command packages under cmd/.

## Inspection and Binary Introspection
Inspect build info
go version -m ./dist/app

# Show module/build info embedded in a binary.

List binary symbols
go tool nm ./dist/app

# Inspect symbols in a compiled binary.

Disassemble binary
go tool objdump ./dist/app

# Disassemble functions from a compiled binary.

Recommended next

No recommendations yet.