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. Thego build command handles dependency resolution, compilation, and linking automatically.
Basic Usage
Output Behavior
Main Packages (Executables)
Main Packages (Executables)
When building a main package, On Windows, adds
go build creates an executable file:.exe extension automatically.Non-Main Packages (Libraries)
Non-Main Packages (Libraries)
When building library packages, This is useful for verifying packages compile successfully.
go build compiles them but discards the result:Custom Output with -o
Custom Output with -o
The
-o flag controls output location:Build Flags
Common Flags
Specify output file or directory
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 detectionSupported on: darwin/amd64, darwin/arm64, linux/amd64, linux/arm64, windows/amd64
Enable memory sanitizer (requires Clang/LLVM)
Enable address sanitizer
Enable code coverage instrumentation
Build Tags
Comma-separated list of build tags
Compiler and Linker Flags
Arguments to pass to the Go compiler
Arguments to pass to the linker
Arguments to pass to the assembler
Cross-Compilation
Common Target Platforms
Linux
macOS
Windows
ARM
Advanced Options
Build Modes
Specify build mode
Module Flags
Module download mode: readonly, vendor, or mod
Use alternate go.mod file
Path Trimming
Remove file system paths from executableResults in reproducible builds and smaller binaries.
Build Examples
Development Builds
Production Builds
Multi-Platform Release
Makefile
Build Performance
Caching
Go automatically caches build artifacts:Parallel Builds
Incremental Builds
Troubleshooting
Build is slow
Build is slow
Binary is too large
Binary is too large
Module version conflicts
Module version conflicts
CI/CD Integration
GitHub Actions
.github/workflows/build.yml
See Also
go Command
Overview of go command
Testing
Testing Go packages
Modules
Managing dependencies
Compiler
Go compiler reference