go
Manage Go source code
TLDR
Download and install a package, specified by its import path
Compile and run a source file (it has to contain a main package)
Compile a source file into a named executable
Compile the package present in the current directory
Execute all test cases of the current package (files have to end with _test.go)
Compile and install the current package
Initialize a new module in the current directory
SYNOPSIS
go command [arguments...]
PARAMETERS
build
Compiles packages and dependencies. Creates an executable binary by default.
clean
Removes object files and cached files from the build cache.
env
Prints Go environment information, such as GOPATH, GOROOT, and architecture details.
fmt
Formats Go source code according to the official Go style guide (gofmt).
get
Adds a dependency to the current module and installs it. Fetches and installs packages and their dependencies.
install
Compiles and installs packages and their dependencies. Places compiled binaries in a standard location (e.g., $GOPATH/bin or $GOBIN).
list
Lists packages or modules, including their properties, dependencies, and build contexts.
mod
Provides subcommands for managing Go modules, including 'tidy', 'init', 'vendor', and 'graph'.
run
Compiles and runs the main package specified by the arguments. Useful for quick execution of Go programs.
test
Runs tests for the packages named by the import paths. Supports various options for verbose output, coverage, and specific test execution.
tool
Runs the Go tool specified by name. Accesses low-level tools like 'compile', 'link', 'asm'.
version
Prints the Go version information, including the release number and build details.
help
Displays help information for a given command or for the go command itself.
DESCRIPTION
The go command is the central utility for managing and interacting with Go source code. It provides a comprehensive suite of subcommands that cover the entire development lifecycle, including building, running, testing, formatting, dependency management, and more.
Developed as an integral part of the Go programming language, this tool simplifies complex tasks such as compiling projects, fetching external modules, and ensuring code quality. It abstracts away much of the underlying complexity of compilation and linking, allowing developers to focus on writing code. The go command is designed to be highly efficient and provides consistent behavior across different operating systems, making it an indispensable tool for Go programmers.
CAVEATS
The behavior of some go subcommands, particularly those related to dependency management (like go get and go mod), has evolved significantly with the introduction and maturation of Go Modules (Go 1.11+). Older Go versions rely more heavily on the GOPATH environment variable. Ensure your Go environment variables (e.g., GOPATH, GOROOT, GOBIN) are correctly configured for optimal use. Network access is frequently required for fetching module dependencies.
GO MODULES
Introduced in Go 1.11 and becoming the default in Go 1.13, Go Modules provide a robust system for dependency management. They eliminate the need for projects to reside within the GOPATH, allowing Go projects to be located anywhere. The go mod subcommand group (e.g., go mod init, go mod tidy, go mod vendor) is central to managing module dependencies, versioning, and vendoring.
ENVIRONMENT VARIABLES
Several environment variables influence the behavior of the go command:
GOPATH: Historically, the workspace for Go projects and dependencies. Now primarily used for tools and binaries.
GOROOT: Specifies the root of the Go SDK installation.
GOBIN: The directory where installed Go programs (executables) are placed.
GOOS and GOARCH: Used for cross-compilation, specifying the target operating system and architecture respectively (e.g., GOOS=linux GOARCH=arm64 go build).
HISTORY
The Go programming language was conceived at Google in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson, with its public announcement in November 2009. From its inception, the go command-line tool has been the primary interface for Go development. Initially, it managed dependencies primarily through the GOPATH workspace model. A significant evolution occurred with Go 1.11 (released in August 2018) and Go 1.13 (defaulting modules), which introduced and solidified the Go Modules system, fundamentally changing how dependencies are managed and how the go command interacts with projects, making it more resilient and explicit.