Documentation Index Fetch the complete documentation index at: https://mintlify.com/golang/go/llms.txt
Use this file to discover all available pages before exploring further.
The go build command compiles packages and their dependencies, creating executable binaries or verifying that packages can be built.
Overview
Building is one of the most common operations in Go development. The go build command handles dependency resolution, compilation, and linking automatically.
Basic Usage
go build [-o output] [build flags] [packages]
Current Package
Specific Packages
# Build current package
go build
# Build with custom output name
go build -o myapp
# Build for current directory
go build .
Output Behavior
Main Packages (Executables)
When building a main package, go build creates an executable file: # Builds executable named after last path component
go build example.com/myapp # Creates: myapp
go build example.com/foo/v2 # Creates: foo
On Windows, adds .exe extension automatically.
Non-Main Packages (Libraries)
When building library packages, go build compiles them but discards the result: # Compiles but doesn't create output file
go build example.com/mylib
This is useful for verifying packages compile successfully.
The -o flag controls output location: # Specific filename
go build -o bin/myapp
# Directory (must end with / or \\)
go build -o bin/ example.com/myapp
# Multiple packages to directory
go build -o bin/ ./cmd/...
Build Flags
Common Flags
Specify output file or directory go build -o myapp
go build -o bin/
Print names of packages as they are compiled
Force rebuilding of packages that are already up-to-date
Print commands but do not run them
Print commands as they are executed
Compilation Control
Number of programs to run in parallel (default: GOMAXPROCS)
Enable data race detection Supported on: darwin/amd64, darwin/arm64, linux/amd64, linux/arm64, windows/amd64
Enable memory sanitizer (requires Clang/LLVM)
Enable code coverage instrumentation
Comma-separated list of build tags go build -tags=integration,postgres
go build -tags "debug trace"
Compiler and Linker Flags
Arguments to pass to the Go compiler # Disable optimizations and inlining
go build -gcflags= '-N -l'
# View optimization decisions
go build -gcflags= '-m'
# For specific package
go build -gcflags= 'example.com/myapp=-N -l'
Arguments to pass to the linker # Set version information
go build -ldflags= "-X main.version=1.0.0"
# Strip debug info and reduce binary size
go build -ldflags= "-s -w"
# Multiple flags
go build -ldflags= "-X main.version=1.0.0 -s -w"
Arguments to pass to the assembler
Cross-Compilation
Set Target Platform
Use GOOS and GOARCH environment variables: # Linux AMD64
GOOS = linux GOARCH = amd64 go build
# Windows
GOOS = windows GOARCH = amd64 go build -o app.exe
# macOS ARM64 (Apple Silicon)
GOOS = darwin GOARCH = arm64 go build
List Supported Platforms
View all supported OS/architecture combinations:
Build for Multiple Platforms
Common cross-compilation script: #!/bin/bash
platforms = ( "linux/amd64" "darwin/amd64" "windows/amd64" )
for platform in "${ platforms [ @ ]}" ; do
platform_split = (${ platform // \/ / })
GOOS = ${ platform_split [0]}
GOARCH = ${ platform_split [1]}
output_name = 'myapp-' $GOOS '-' $GOARCH
if [ $GOOS = "windows" ]; then
output_name += '.exe'
fi
GOOS = $GOOS GOARCH = $GOARCH go build -o bin/ $output_name
done
Linux GOOS = linux GOARCH = amd64 go build
GOOS = linux GOARCH = arm64 go build
macOS GOOS = darwin GOARCH = amd64 go build
GOOS = darwin GOARCH = arm64 go build
Windows GOOS = windows GOARCH = amd64 go build
GOOS = windows GOARCH = 386 go build
ARM GOOS = linux GOARCH = arm go build
GOOS = linux GOARCH = arm64 go build
Advanced Options
Build Modes
Specify build mode # Default executable
go build -buildmode=exe
# Shared library
go build -buildmode=shared
# C shared library
go build -buildmode=c-shared -o mylib.so
# C static library
go build -buildmode=c-archive
# Plugin
go build -buildmode=plugin
Module Flags
Module download mode: readonly, vendor, or mod # Read-only mode (default in most cases)
go build -mod=readonly
# Use vendor directory
go build -mod=vendor
# Allow module updates
go build -mod=mod
Use alternate go.mod file go build -modfile=go.dev.mod
Path Trimming
Remove file system paths from executable Results in reproducible builds and smaller binaries.
Build Examples
Development Builds
Quick Development Build
Debug Build
# Fast build for local testing
go build -o bin/myapp
# With race detector
go build -race -o bin/myapp
# Verbose output
go build -v -o bin/myapp
Production Builds
Optimized Production Build
Static Binary
# Strip debug info, reduce size
go build -ldflags= "-s -w" -trimpath -o bin/myapp
# With version info
VERSION = $( git describe --tags )
go build -ldflags= "-s -w -X main.version= $VERSION " -trimpath -o bin/myapp
.PHONY: build-all
build-all:
@echo "Building for multiple platforms..."
GOOS = linux GOARCH = amd64 go build -ldflags= "-s -w" -o dist/myapp-linux-amd64
GOOS = linux GOARCH = arm64 go build -ldflags= "-s -w" -o dist/myapp-linux-arm64
GOOS = darwin GOARCH = amd64 go build -ldflags= "-s -w" -o dist/myapp-darwin-amd64
GOOS = darwin GOARCH = arm64 go build -ldflags= "-s -w" -o dist/myapp-darwin-arm64
GOOS = windows GOARCH = amd64 go build -ldflags= "-s -w" -o dist/myapp-windows-amd64.exe
Caching
Go automatically caches build artifacts:
# View cache location
go env GOCACHE
# Clear build cache
go clean -cache
# Clear module cache
go clean -modcache
Parallel Builds
# Increase parallelism
go build -p 8
# Set via environment
export GOMAXPROCS = 8
go build
Incremental Builds
# Only rebuild changed packages (default)
go build
# Force full rebuild
go build -a
Troubleshooting
# Check what's being compiled
go build -x
# Increase parallelism
go build -p 8
# Check cache
go env GOCACHE
# Strip debug symbols
go build -ldflags= "-s -w"
# Use UPX compression
go build -ldflags= "-s -w" && upx myapp
# Update dependencies
go get -u
go mod tidy
# Use specific version
go get example.com/pkg@v1.2.3
CI/CD Integration
GitHub Actions
.github/workflows/build.yml
name : Build
on : [ push , pull_request ]
jobs :
build :
runs-on : ubuntu-latest
steps :
- uses : actions/checkout@v4
- uses : actions/setup-go@v5
with :
go-version : '1.21'
- name : Build
run : go build -v ./...
- name : Build release
run : |
go build -ldflags="-s -w" -trimpath -o bin/myapp
See Also
go Command Overview of go command
Testing Testing Go packages
Modules Managing dependencies
Compiler Go compiler reference