An editor, gofmt, go vet and go doc
A Go editor is any editor plus three commands. gofmt decides the layout of the code so that no review ever discusses it, go vet reads the program for mistakes that compile, and go doc prints the documentation of a package from the source that is installed. None of them needs a project, a daemon or a configuration file.
The editor matters less than the loop it makes cheap: save, format, build, run. Every editor with Go support runs gofmt on save and go vet on build, because both are one command with a machine-readable answer.
gofmt
The formatter is not configurable and that is the feature: two programmers who disagree about tabs still produce the same file. gofmt -l lists what it would change, gofmt -d prints the patch, and gofmt -w writes it. go fmt ./... runs the same formatter over a package and its subpackages.
package main
import "fmt"
func main() {
fmt.Println("the program runs; gofmt only changes the layout")
}
go run format.go
printf 'package main\nfunc main(){println("hi")}\n' > messy.go
gofmt -l .
gofmt -d messy.go
rm messy.go
gofmt -l .
the program runs; gofmt only changes the layout
messy.go
diff messy.go.orig messy.go
--- messy.go.orig
+++ messy.go
@@ -1,2 +1,3 @@
package main
-func main(){println("hi")}
+
+func main() { println("hi") }
The diff is the whole style guide: tabs for indentation, a space after a keyword, a newline at the end of the file. A continuous integration job runs gofmt -l and fails when the answer is not empty, which is cheaper than a review comment.
go vet catches what compiles
The program below builds, runs and prints something. The second line is wrong: the format verb asks for a string and gets an integer, so the formatter writes a diagnostic into the output instead of a value. go vet reads the same program and names the mistake at the line.
package main
import "fmt"
func main() {
count := 3
fmt.Printf("%d items\n", count)
fmt.Printf("%s\n", count)
}
go run vet.go
go vet vet.go
3 items
%!s(int=3)
vet.go:8:14: fmt.Printf format %s has arg count of wrong type int
The first line of the program's output is correct. The second is %!s(int=3), which is how fmt reports a verb that does not match its argument: the mistake is visible in the output, but only if the reader knows what to look for. vet knows. Its exit status is non-zero, so a build pipeline stops on it.
go doc
The documentation of the standard library is the source, and go doc prints it without a browser. It also works for the packages of the current module, which is how a team reads its own API the way a reader will.
package main
import "fmt"
func main() {
fmt.Printf("%d items\n", 3)
}
go doc fmt.Println
go doc -src fmt.Println
go list std | wc -l
package fmt // import "fmt"
func Println(a ...any) (n int, err error)
Println formats using the default formats for its operands and writes to
standard output. Spaces are always added between operands and a newline
is appended. It returns the number of bytes written and any write error
encountered.
package fmt // import "fmt"
// Println formats using the default formats for its operands and writes to standard output.
// Spaces are always added between operands and a newline is appended.
// It returns the number of bytes written and any write error encountered.
func Println(a ...any) (n int, err error) {
return Fprintln(os.Stdout, a...)
}
356
go doc -all prints every exported name of a package, go doc -src fmt.Println prints the implementation, and go doc fmt.Printf prints one symbol. The number of packages in the standard library is the answer to "what do I get without a dependency", and it changes with the Go version.
The editor
Whichever editor you use, the four commands that must be one keystroke away are gofmt -w, go vet, go build and go test. The language server, gopls, adds completion and jump-to-definition on top of the same tools; it is a separate program and its version moves independently.
The next lesson writes the first real program, and the two after it put that program into a module and a package. The lesson on vet and linters comes back to staticcheck and golangci-lint, which go further than vet and are not part of the toolchain.
Exercises
- Run gofmt -d on a file of your own that you have formatted by hand. Which of its changes surprise you?
- Add a third Printf to the program above with %d and a string argument. What does the program print, and what does vet say?
- Read the documentation of strings.Builder with go doc strings.Builder. Which method writes a rune?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
gofmt -l .print?Answer
The files the formatter would change
The -l flag lists files; -d prints the patch and -w rewrites them.
-
What does
go vetreport?Answer
Suspicious code that compiles
vet is a set of analyzers over the program: format verbs, lost locks, unreachable code and the like.
-
What does this program print?
package main import "fmt" func main() { fmt.Printf("%d items\\n", 3) }Answer
3 items
The verb matches the argument, so the integer is written where %d stands.
-
What does this program print?
package main import "fmt" func main() { fmt.Printf("%s\\n", 3) }Answer
%!s(int=3)
fmt writes a diagnostic for a verb that does not fit the argument, and the program still runs.
-
What is the exit status of
gofmt -lwhen a file needs formatting?Answer
0: it only lists files
gofmt -l lists the files and exits 0; the CI job checks whether the output is empty.
-
Which command prints the documentation of one function?
Answer
go doc fmt.Println
go doc takes a package or a package and a symbol, and reads it from the installed source.
-
What does
go doc -src strings.Builderprint?Answer
The implementation of the type
-src prints the source of the symbol instead of its documentation comment.
-
What does
go fmt ./...do?Answer
It runs gofmt over the packages of the current module
go fmt is gofmt applied to the files of the named packages, and ./... is the module and its subfolders.
-
Why does a team run gofmt instead of writing a style guide?
Answer
It produces the same file for everyone, so layout never reaches a review
The formatter is not configurable, so the argument about layout is settled by the tool.
-
Which tool adds completion and jump-to-definition to an editor?
Answer
gopls
gopls is the language server; it drives the same compiler and documentation under the editor.
The code on this page was run with go 1.26.10 on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev