HowtoGo
Home / Basics / The go tool
Basics

The go tool

Go ships as a single go binary that builds, tests, formats, generates, documents, and profiles. Every subcommand, with its flags and a worked example.

One binary does the whole job: compiling, running, testing, formatting, dependency management, documentation, and profiling. There is no separate build tool, formatter, or package manager to install.

Click any command for its flags and a worked example, or filter to jump straight to one.

go 32 commands and the flags each one takes

Build and run

4
  • go run <pkg|file...>
    $ go run .

    Compiles and runs a program, leaving no binary behind.

  • go build [pkg...]
    $ go build -o bin/app .

    Compiles packages into a binary without running it.

  • go install <pkg>[@version]
    $ go install golang.org/x/tools/cmd/stringer@latest

    Builds a command and puts the binary in GOBIN.

  • go clean
    $ go clean -cache -testcache

    Removes object files and cached build artifacts.

Test and check

4
  • go test [pkg...]
    $ go test ./...

    Builds and runs the tests in each named package.

  • go vet [pkg...]
    $ go vet ./...

    Reports suspicious constructions the compiler accepts.

  • go fix [pkg...]
    $ go fix ./...

    Rewrites code that uses old APIs to the current ones.

  • go bug
    $ go bug

    Opens a browser to file a Go issue, with your environment filled in.

Format and generate

3
  • go fmt [pkg...]
    $ go fmt ./...

    Formats the source in each named package and prints what it changed.

  • gofmt -l -d <path>
    $ gofmt -l -d .

    Lists unformatted files and prints the diff, changing nothing.

  • go generate [pkg...]
    $ go generate ./...

    Runs the commands named in //go:generate comments.

Documentation and environment

4
  • go doc [pkg][.<name>]
    $ go doc bufio.Scanner

    Prints the documentation for a package, type, function, or method.

  • go version [-m <binary>]
    $ go version -m ./app

    Prints the Go version, or the versions built into a binary.

  • go env [-w] [var...]
    $ go env GOMODCACHE

    Prints the Go environment, or sets a value with -w.

  • go list [pkg...]
    $ go list -m -u all

    Reports information about packages and modules.

Modules and dependencies

9
  • go mod init <module-path>
    $ go mod init example.com/hello

    Creates a go.mod file in the current directory.

  • go mod tidy
    $ go mod tidy

    Adds missing module requirements and drops unused ones.

  • go get <pkg>[@version]
    $ go get github.com/jackc/pgx/v5@v5.10.0

    Adds, upgrades, or removes a dependency in go.mod.

  • go mod download [modules]
    $ go mod download

    Downloads modules into the local module cache.

  • go mod verify
    $ go mod verify

    Checks that cached dependencies match the hashes in go.sum.

  • go mod why <pkg>
    $ go mod why golang.org/x/text

    Explains why a package or module is needed.

  • go mod graph
    $ go mod graph

    Prints the module dependency graph, one edge per line.

  • go mod edit <flags>
    $ go mod edit -replace=old.dev/x=../x

    Edits go.mod from a script, without a text editor.

  • go mod vendor
    $ go mod vendor

    Copies every dependency into a vendor directory.

Workspaces

3
  • go work init [dirs...]
    $ go work init ./api ./worker

    Creates a go.work file covering several modules at once.

  • go work use [dirs...]
    $ go work use ./billing

    Adds a module directory to the workspace.

  • go work sync
    $ go work sync

    Pushes the workspace's resolved versions back into each module's go.mod.

Tools and diagnostics

5
  • go tool [name] [args]
    $ go tool pprof cpu.out

    Runs one of the tools shipped with the toolchain.

  • go tool pprof <profile>
    $ go tool pprof -http=:8081 cpu.out

    Opens a CPU, memory, or block profile for analysis.

  • go tool trace <trace file>
    $ go tool trace trace.out

    Opens an execution trace, showing goroutines and the scheduler.

  • go tool cover <flags>
    $ go tool cover -html=cover.out

    Renders a coverage profile as annotated source.

  • go telemetry <on|off|local>
    $ go telemetry off

    Controls whether the toolchain uploads usage telemetry.