Go CLI Quickstart
Modules
Go modules are the standard way to manage dependencies and versioning.
Initialize a module
Creates a go.mod file that tracks your module name and dependencies.
1go mod init github.com/user/myprojectTidy dependencies
Downloads missing dependencies, removes unused ones, and updates go.mod and go.sum.
1go mod tidyDownload dependencies
Fetches all dependencies to the local cache without building.
1go mod downloadVendor dependencies
Copies all dependencies into a vendor/ directory. Useful for reproducible builds or offline work.
1go mod vendorBuilding & Running
Run without building a binary
Compiles and runs in one step. Useful during development.
1go run main.go
2go run . # run package in current dir
3go run ./cmd/myapp # run package in subdirectoryBuild a binary
Compiles the package and outputs an executable. Default name matches the module or directory.
1go build # outputs binary in current dir
2go build -o myapp # specify output name
3go build ./cmd/myapp # build a specific packageInstall globally
Builds and places the binary in $GOPATH/bin (or $GOBIN). Make sure this is in your $PATH.
1go install # install current module
2go install github.com/user/tool@latest # install remote toolTesting
Run tests
Runs all tests in the current package. Use ./... to run tests recursively.
1go test # current package
2go test ./... # all packages recursively
3go test ./pkg/... # all packages under pkg/Verbose output
Shows each test name and result as it runs.
1go test -v ./...Run specific tests
Use -run with a regex to filter test names.
1go test -run TestFoo # tests matching "TestFoo"
2go test -run TestFoo/subtest # specific subtestCoverage
Generate a coverage report. Use -coverprofile to save results for viewing.
1go test -cover ./...
2go test -coverprofile=coverage.out ./...
3go tool cover -html=coverage.out # view in browser
4go tool cover -func=coverage.out # print per-function statsRace detector
Detects data races at runtime. Slower but catches concurrency bugs.
1go test -race ./...Benchmarks
Runs benchmark functions (named BenchmarkXxx). -bench . runs all benchmarks.
1go test -bench .
2go test -bench BenchmarkFoo -benchmem # include memory statsFormatting & Linting
Format code
Rewrites source files to canonical Go style. Always run before committing.
1go fmt ./...Vet for suspicious code
Static analysis that catches common mistakes (printf format errors, unreachable code, etc.).
1go vet ./...Dependencies
Add a dependency
Import it in your code, then run go mod tidy, or fetch it explicitly.
1go get github.com/spf13/cobraAdd a specific version
Pin to a version, commit, or branch.
1go get github.com/spf13/cobra@v1.8.0
2go get github.com/spf13/cobra@latest
3go get github.com/user/repo@commitshaUpdate dependencies
Update a single dependency or all dependencies.
1go get -u github.com/spf13/cobra # update one
2go get -u ./... # update all direct deps
3go get -u all # update everythingRemove a dependency
Delete the import from your code, then tidy.
1go mod tidyList dependencies
Show all direct and indirect dependencies.
1go list -m allWhy is this dependency here?
Trace why a module is required.
1go mod why github.com/some/moduleInformation & Docs
Show documentation
View docs for a package or symbol in the terminal.
1go doc fmt
2go doc fmt.Println
3go doc -all fmt # full package docsList packages
Show packages matching a pattern.
1go list ./... # all packages in module
2go list -m -versions github.com/spf13/cobra # available versionsEnvironment info
Print Go environment variables. Useful for debugging path issues.
1go env
2go env GOPATH GOBIN GOOS GOARCHCross-Compilation
Go can compile for different OS/architecture combinations by setting GOOS and GOARCH.
1GOOS=linux GOARCH=amd64 go build -o myapp-linux
2GOOS=windows GOARCH=amd64 go build -o myapp.exe
3GOOS=darwin GOARCH=arm64 go build -o myapp-macList all supported platforms:
1go tool dist listWorkspaces
Workspaces let you work on multiple modules simultaneously without publishing them.
Initialize a workspace
Creates a go.work file.
1go work initAdd modules to workspace
Adds local module directories to the workspace.
1go work use ./myapp ./mylibSync workspace
Updates the workspace to match module requirements.
1go work syncUseful Flags
Build flags
1go build -v # print package names as they compile
2go build -x # print commands being executed
3go build -ldflags "-s -w" # strip debug info (smaller binary)
4go build -tags mytag # build with custom build tagTest flags
1go test -timeout 30s # fail if tests take too long
2go test -short # skip long-running tests
3go test -count 1 # disable test caching
4go test -shuffle on # randomize test order