The Go Toolchain
You've used go run, go build, and go mod init. Those are all the same
binary — go — dispatching on its first argument. It's worth knowing the
rest of its verbs, because they cover testing, formatting, and reading
documentation, all without leaving the terminal.
Testing
Go's testing support is built in — no separate framework to install. A test
file ends in _test.go and contains functions named TestXxx:
// main_test.go
package main
import "testing"
func TestAdd(t *testing.T) {
if got := add(2, 3); got != 5 {
t.Errorf("add(2, 3) = %d; want 5", got)
}
}
Run all tests in the current module with:
go test ./...
The ./... means "this directory and everything below it." Every time this
site checks your solution against hidden tests, it's running something
close to this command behind the scenes — the "Run tests" button in the
browser and go test ./... on your machine are doing the same job. You'll
write plenty of your own tests in the Testing in Depth chapter later on.
Formatting
Go ships an opinionated formatter, and the community treats its output as
non-negotiable — there's no style debate to have, because gofmt already
settled it.
gofmt -l . # list files that aren't formatted
gofmt -w . # rewrite them in place
go fmt ./... # same idea, module-aware
This isn't cosmetic preference; it's why every Go codebase you'll ever open
looks the same, down to tab-vs-space and brace placement. Most editors
with Go support run gofmt automatically on save, so in practice you'll
rarely invoke it by hand — but it's worth knowing it's there, and why your
code always comes out looking the same as everyone else's.
Vetting
go vet catches things that compile fine but are almost certainly bugs —
a Printf call with mismatched format verbs, a struct copied while its
mutex is locked, an unreachable branch. (One classic gotcha it used to
flag — a loop variable captured by a closure — Go 1.22 fixed at the
language level, so on modern Go you rarely hit it.)
go vet ./...
It's cheap to run and catches a category of mistake that tests sometimes miss. Many editors run it continuously and underline the results as you type.
Reading docs from the terminal
You don't need a browser tab open to check what a stdlib function does:
go doc fmt.Println
go doc net/http
go doc <package> prints an overview of everything exported from that
package; go doc <package>.<Name> drills into one function, type, or
constant. It's often faster than searching the web, and it always matches
the version of Go you have installed.
That's the core loop: write code, go run it, go test it, let gofmt
keep it tidy, and go vet double-check it. Everything from here on in this
course exercises that same loop — you now have the tools to run it
yourself, outside the browser.