go mod, go fmt/vet/doc, workspaces, build, and ship.
go fmt, vet, doc, test, build
Go ships with one of the best tool-belts of any language. You'll use all of these every day.
go fmt — there is one style
gofmt reads your code and rewrites it in the canonical layout. No options, no
config file. Run it from the command line or have your editor run it on save.
gofmt -w . # rewrite all .go files in place
goimports -w . # like gofmt, but also fixes imports
The official style is the only style. Disagreements about brace placement and indent width simply don't exist in Go.
go vet — static checks
vet looks for suspicious code that compiles but probably isn't right —
mismatched Printf arguments, copy of a sync.Mutex, unreachable code, and so on.
go vet ./...
Run it in CI. You can also add golangci-lint for a much richer set of
checks (more than 50 linters bundled).
go doc — read documentation locally
go doc fmt
go doc fmt.Sprintf
go doc github.com/google/uuid.New
It reads the comment block directly above each exported identifier — the same comments that show up on pkg.go.dev. Write short, helpful sentences:
// New returns a random UUID.
func New() UUID { ... }
go test — recap
go test ./... # everything
go test -run TestName # one test
go test -v -race -cover ./... # verbose, race detector, coverage
go test -bench=. -benchmem # benchmarks
go test -fuzz=Name # fuzz
The race detector (-race) is your friend. Run it in CI on every PR.
go build — make a binary
go build # build current package, produces ./packagename
go build -o myapp . # custom output name
go install . # install into $GOBIN
Cross-compilation
This is one of Go's superpowers. To build for Linux on a Mac:
GOOS=linux GOARCH=amd64 go build -o myapp-linux
GOOS=darwin GOARCH=arm64 go build -o myapp-mac
GOOS=windows GOARCH=amd64 go build -o myapp.exe
No cross-compiler installation. No toolchains. Set two env vars; get a binary.
go run a tool without installing
Need a one-off tool like staticcheck or mockgen? Skip the install step:
go run honnef.co/go/tools/cmd/staticcheck@latest ./...
go run pkg@version fetches, builds, and runs the command in one go. Great in
CI scripts and Makefiles where you don't want a global install.
go install for tools
Installs a tool binary into $GOBIN (default $GOPATH/bin):
go install github.com/air-verse/air@latest
Make sure $GOBIN is in your $PATH.