Quidest?

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/myproject

Tidy dependencies

Downloads missing dependencies, removes unused ones, and updates go.mod and go.sum.

1go mod tidy

Download dependencies

Fetches all dependencies to the local cache without building.

1go mod download

Vendor dependencies

Copies all dependencies into a vendor/ directory. Useful for reproducible builds or offline work.

1go mod vendor

Building & 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 subdirectory

Build 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 package

Install 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 tool

Testing

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 subtest

Coverage

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 stats

Race 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 stats

Formatting & 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/cobra

Add 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@commitsha

Update 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 everything

Remove a dependency

Delete the import from your code, then tidy.

1go mod tidy

List dependencies

Show all direct and indirect dependencies.

1go list -m all

Why is this dependency here?

Trace why a module is required.

1go mod why github.com/some/module

Information & 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 docs

List packages

Show packages matching a pattern.

1go list ./...                   # all packages in module
2go list -m -versions github.com/spf13/cobra   # available versions

Environment info

Print Go environment variables. Useful for debugging path issues.

1go env
2go env GOPATH GOBIN GOOS GOARCH

Cross-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-mac

List all supported platforms:

1go tool dist list

Workspaces

Workspaces let you work on multiple modules simultaneously without publishing them.

Initialize a workspace

Creates a go.work file.

1go work init

Add modules to workspace

Adds local module directories to the workspace.

1go work use ./myapp ./mylib

Sync workspace

Updates the workspace to match module requirements.

1go work sync

Useful 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 tag

Test 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