gofmt, vet, and documentation
Formatting is not debated in Go, and go vet catches suspicious code before review.
Go has almost no style arguments about braces and indentation. The command gofmt brings a file to one shape: tabs for indentation, spaces for alignment, and the required semicolon is inserted by the compiler itself. You do not write it. A repository is not formatted "however is easier on the eye". Before a commit you run gofmt -w ., or you save in an editor that calls the same formatter.
go vet ./... is not a linter of taste. It is a set of compiler-level checks: wrong format strings in fmt.Printf, suspicious self-assignment, unreachable code, mistakes in tests. What vet considers an error usually is an error. Third-party linters such as staticcheck are useful, but a Middle developer trusts vet first, not a hundred enabled rules.
Documentation lives next to the code. A comment on an exported name starts with that name:
// ParseConfig reads YAML from path and returns a ready Config.
func ParseConfig(path string) (Config, error) {
return Config{}, nil
}
go doc fmt.Println and the site pkg.go.dev show exactly these comments. A comment like "// helper" above an exported function is useless: the documentation will be empty in meaning. Unexported names are commented too, when the intention cannot be read from the name, but the public contract is always documented.
A useful minimum of commands from the module root:
gofmt -w .
go vet ./...
go test ./...
go doc fmt.Println
go help answers about the commands themselves. If you forgot the flags of go test, that is faster than hunting for a random article from five years ago.
The habit that marks Middle
Formatting and vet sit in the same place as the tests: locally before a push, and in CI. A person should not have to leave the comment "fix the indent". If gofmt changes a file, that is not a question of taste. It is an uncommitted diff.
Practice
Who formats the code
How does a Go team usually bring code to one shared shape?
- Agree in the README and watch it during review
- Run gofmt, usually when the file is saved
- Turn on prettier with a JavaScript preset
- Write a semicolon after every line, otherwise gofmt will refuse
What vet is for
What best describes
go vet?- A replacement for go test when there are no tests yet
- A check of suspicious code that nevertheless compiles
- An installer of dependencies from go.sum
- A formatter that is older than gofmt
A comment on the API
The function is named
ParseConfig. Which word must its documentation comment start with so thatgo docshows a proper summary? Write one English word — the name the comment starts with.