The go toolchain
| go build | |
| go vet finds bugs | |
| gofmt your code | |
| Read docs locally |
go build
The go tool builds, tests, formats, and documents your code, so a fresh machine needs little beyond the toolchain itself. Start with build: from the module root, one command compiles every package and drops a runnable binary next to your code.
package main
import "fmt"
func main() {
fmt.Println("hello, Go")
}
go build -o hello
./hello
hello, Go
The first command compiles the program into a file named hello, and the second runs it. While experimenting, go run main.go compiles to a temporary file and runs it in one step.
go vet finds bugs
The compiler accepts many suspicious programs that still run, such as a Printf call with fewer arguments than verbs. The vet command scans for these classic mistakes and reports them before they reach production.
package main
import "fmt"
func main() {
name := "Go"
fmt.Printf("%s %s\n", name)
}
go vet
# example.com/hello
./main.go:7:5: fmt.Printf format %s reads arg #2, but call has 1 arg
The format asks for two strings, but the call passes one, so vet points at the exact line and column. Run vet over your module before every review: it costs a second and catches what eyes skip.
gofmt your code
The gofmt tool rewrites spacing and alignment into the one true style, ending debates about indentation and brace placement. The -l flag lists files that differ from the standard, and -w rewrites them in place, so run it before every commit.
gofmt -l .
main.go
The output names main.go as a file that needs formatting. After gofmt -w main.go, the same listing prints nothing, which means the whole module matches the standard style.
Read docs locally
Your installed toolchain carries its own manual. The go doc command prints documentation for any package or symbol without opening a browser, which helps when the network is gone or a signature slips your mind.
go doc fmt.Println
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.
One command shows the signature plus a one-paragraph summary. Explore from there: go doc fmt opens the whole package, and go doc time.Duration narrows down to a single type.
Next: Testing with go test
Article author: Arthur Isaev