Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 147 ยท Getting Started

The go Command and Basic Tooling

Learn the everyday Go tools - go fmt, go vet, go doc, go env, go version and go help, plus gopls, editors, golangci-lint and a simple daily workflow.

One reason Go feels productive is that the language ships with its tools. Formatting, static checks, documentation, dependency management and testing are all subcommands of a single program: go. There is nothing to configure and no competing style guides to choose from.

This lesson introduces the tools you will use every day: go fmt, go vet, go doc, go env, go version and go help. We also look at the language server gopls that powers editor features and at the popular linter golangci-lint. The terminal sessions below are read-only because they run on your machine, but the Try it examples run the same machinery (the formatter and the parser are ordinary packages in the standard library) so you can see what the tools do.

Asking for help

The go command documents itself:

Terminal
$ go version
go version go1.27.3 linux/amd64

$ go help
Go is a tool for managing Go source code.

Usage:

	go <command> [arguments]
...

$ go help build        # details about one command
$ go help environment  # all environment variables
$ go help packages     # how package patterns like ./... work

go version tells you which toolchain you run. The output shows the version, the operating system and the CPU architecture. go help topic prints the manual for a command or a subject. It is faster than searching the web, and it always matches your installed version.

Patterns: ./... and friends

Most commands accept package patterns. . is the package in the current folder, ./cmd/app is a package in a subfolder, and ./... means "this folder and everything below it". You will type go vet ./... and go test ./... constantly.

go fmt: one style, no debate

go fmt rewrites your files in the standard style, using the same engine as the gofmt program. It never changes what the code means, only the layout: indentation with tabs, alignment of comments and struct fields, spacing around operators, and so on.

Terminal
$ go fmt ./...          # reformat every package, print the names of changed files
$ gofmt -l .            # only list files that need formatting
$ gofmt -d main.go      # show a diff of what would change
$ gofmt -s -w .         # also apply safe simplifications, and write the result

Most editors run the formatter every time you save. You can see what it does with the same code the tool uses, the go/format package:

Go What gofmt does to messy code

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Notice what happened: tabs for indentation, spaces around :=, < and after commas, point{ x:1,y :2 } tidied into point{x: 1, y: 2}, and the if body broken onto its own lines. If the code has a syntax error, format.Source (like gofmt) refuses and reports it, which makes the formatter a fast syntax check too.

Run go fmt before every commit, or turn on "format on save" in your editor. Many teams also fail their continuous-integration build when gofmt -l . prints anything.

go vet: find code that compiles but looks wrong

The compiler guarantees that your program is valid Go. It does not promise that it is a sensible one. go vet runs a collection of static checks for mistakes that are almost always bugs:

  • Printf-style calls with the wrong verbs or the wrong number of arguments,
  • copying a value that contains a sync.Mutex,
  • unreachable code and useless comparisons (x == x),
  • struct tags with a wrong format, misuse of defer, and loop variables captured by goroutines in older code,
  • calls to append or fmt.Sprintf whose result is ignored.

The following program compiles and runs, but go vet would flag three lines. Run it first, then read the vet output below:

Go Compiles fine, but vet complains

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

The output contains %!s(int=3) and %!d(MISSING), which are the signs of the bugs. Here is what go vet prints on your machine for the same file (line numbers refer to this program; the exact wording may change slightly between releases):

TEXT
$ go vet ./...
# example
./main.go:9:21: fmt.Printf format %s has arg count of wrong type int
./main.go:10:14: fmt.Printf format %d reads arg #1, but call has 0 args
./main.go:12:2: result of fmt.Sprintf call not used

go test runs a small subset of vet automatically, but running go vet ./... regularly (and in CI) is a habit worth building. Vet and Linting covers what else is available.

go doc: documentation in your terminal

go doc prints the doc comments we wrote in Comments and Documentation, for the standard library and for your own packages:

Terminal
$ go doc strings                  # package overview
$ go doc strings.ToUpper          # one function
$ go doc strings.Builder          # a type with its methods
$ go doc -all strings             # everything in the package
$ go doc time.Duration.Seconds    # one method

Online, the same information is at pkg.go.dev, where you can read any public Go package, including its examples and its source. Behind the scenes it is just the standard library parsing Go files, and you can do that too:

Go Reading declarations and doc comments with go/parser

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

This is how documentation tools, linters, code generators and your editor understand a Go file. The packages go/ast, go/parser, go/token and go/format are part of the standard library, so writing your own small Go tools is realistic.

go env: where things live

go env prints the settings the toolchain uses, which are mostly environment variables with sensible defaults:

Terminal
$ go env GOOS GOARCH     # the target system for builds
linux
amd64
$ go env GOROOT          # where the Go installation lives
$ go env GOPATH          # your workspace for downloaded modules and installed tools
$ go env GOMODCACHE      # where downloaded dependencies are cached
$ go env -w GOFLAGS=-mod=readonly   # permanently change a setting (stored in a file)
$ go env -json           # everything, as JSON

The ones that matter most early on: GOOS and GOARCH select the target for cross-compiling (How Go Works), GOPATH is where go install puts binaries (in its bin folder, which you add to your PATH), and GOPROXY is the server dependencies are fetched through. The toolchain can also download and switch to a newer Go version automatically when a project requires it, controlled by GOTOOLCHAIN (more in Modules).

Editors and gopls

Go support in editors does not live in each editor. It comes from one program, the language server gopls, maintained by the Go team and used through the Language Server Protocol. It provides completion, go-to-definition, find references, rename, signature help, automatic imports, and formatting on save.

  • Visual Studio Code: install the official Go extension. It installs and updates gopls for you.
  • GoLand (JetBrains) is a full IDE with its own analysis.
  • Neovim, Emacs, Helix, Sublime and others connect to gopls through their LSP clients.

One feature deserves a mention: gopls adds and removes import lines for you when you save, so you rarely write imports by hand.

Linting with golangci-lint

go vet only reports things that are almost certainly wrong. For style, complexity and subtler bugs, many teams add golangci-lint, a community tool that runs dozens of linters (including staticcheck, errcheck for ignored errors, and gosec for security) at once. It reads a .golangci.yml file where you choose which checks to run:

YAML
version: "2"
linters:
  enable:
    - errcheck
    - staticcheck
    - revive
Terminal
$ golangci-lint run ./...

It is not part of Go itself, so you install it separately. Start with go vet, add a linter when your team needs one, and keep its rules small.

A daily workflow

A simple loop covers most of your work:

  1. Edit code. Your editor formats it on save.
  2. go run . to try it (or go build ./... to check everything compiles).
  3. go vet ./... to catch suspicious code.
  4. go test ./... to run your tests (Testing covers them).
  5. go mod tidy after adding or removing imports, to keep go.mod accurate.

Here is a final demonstration of how fast Go's feedback is. This program checks the build you run on, which is something every tool in this lesson has in common: they all know your toolchain version and target.

Go Which toolchain is this?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

debug.ReadBuildInfo returns the module and dependency information that go build embeds in every binary, and tools such as go version -m ./program read the same data.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which command checks for suspicious code that the compiler accepts, such as a Printf with the wrong verb?

    go vet runs static analyses for likely bugs. go fmt only changes layout, go doc prints documentation and go env prints settings.
  2. 2. What does the pattern ./... mean?

    The three dots are a wildcard for 'all subdirectories', so go vet ./... checks the whole project.
  3. 3. How do you read the documentation of strings.Builder in a terminal?

    go doc prints the doc comments for packages, types, functions and methods. go help documents the go command itself.
  4. 4. What is gopls?

    gopls is the Language Server Protocol implementation for Go. Editors talk to it for completion, navigation, rename and formatting.
  5. 5. What does gofmt change?

    gofmt only reformats whitespace and alignment. A program means the same before and after formatting.

Summary

  • Everything starts with go help; go version and go env show your toolchain and settings.
  • go fmt (or gofmt -l, -d, -w) applies the one standard style. Format on save in your editor.
  • go vet ./... finds code that compiles but is almost certainly wrong, such as bad Printf verbs.
  • go doc shows documentation from your terminal; pkg.go.dev does the same online.
  • gopls is the language server behind Go support in every major editor, and golangci-lint bundles many linters when you need more.
  • The standard library exposes go/format, go/parser and go/ast, so you can build your own tools.

Next: Variables and Zero Values starts the next chapter and teaches how Go stores data.