go command is the primary tool for managing Go source code. It provides a unified interface for compiling, testing, installing, and managing Go packages and modules.
Usage
Available Commands
The go command provides the following subcommands:build
Compile packages and dependencies
test
Test packages
run
Compile and run Go program
install
Compile and install packages
get
Add dependencies to current module
mod
Module maintenance
work
Workspace maintenance
fmt
Format package sources with gofmt
Build and Compilation
build- Compile packages and dependenciesinstall- Compile and install packages and dependenciesclean- Remove object files and cached filesrun- Compile and run Go program
Testing and Analysis
test- Test packagesvet- Report likely mistakes in packagesdoc- Show documentation for package or symbol
Module Management
get- Add dependencies to current module and install themmod- Module maintenancework- Workspace maintenance
Code Generation and Formatting
fmt- Format package sources with gofmtgenerate- Generate Go files by processing sourcefix- Apply fixes suggested by static checkers
Information and Environment
env- Print Go environment informationlist- List packages or modulesversion- Print Go versiontool- Run specified go tool
Other Commands
bug- Start a bug reporttelemetry- Manage telemetry data and settings
Common Build Flags
Many go commands share a common set of build flags:Basic Flags
Compilation Flags
Advanced Flags
Environment Variables
Key Environment Variables
GOROOT- Location of the Go installationGOPATH- Workspace location (default:$HOME/go)GOBIN- Directory where go install places binariesGOOS- Target operating systemGOARCH- Target architectureGO111MODULE- Module support modeGOPROXY- Module proxy URLGOPRIVATE- Patterns for private modules
Examples
Building Programs
Running Programs
Getting Help
Help Topics
The go command includes extensive documentation on various topics:buildconstraint- Build constraintsbuildjson- Build -json encodingbuildmode- Build modesc- Calling between Go and Ccache- Build and test cachingenvironment- Environment variablesfiletype- File typesgo.mod- The go.mod filegopath- GOPATH environment variablegoproxy- Module proxy protocolimportpath- Import path syntaxmodules- Modules, module versions, and morepackages- Package lists and patternstestflag- Testing flagstestfunc- Testing functions
See Also
Building Go Programs
Learn about building Go applications
Testing
Testing Go packages
Modules
Working with Go modules
Workspaces
Multi-module workspaces