Skip to the workspace
Pebellslearn by building
Introduction to Go  ›  Capstone Project

The go Command

`go` command 14 minOne go command runs, builds, formats, checks, tests and documents code, so each daily task maps to one subcommand.

golang / The go Command

Explore each scene at your own pace.

01 · The rule

The rule

The go Command

One go command runs, builds, formats, checks, tests and documents code, so each daily task maps to one subcommand.

Compare the work performed by go run, build, test and fmt.

With tally set up, you will spend your day typing go followed by one word. There is no separate build tool, formatter or test runner to install: they are all subcommands of the same go command.

The rule

One task, one subcommand: run, build, fmt, vet, test, doc.

Command What it does
go run . Compiles the package in this folder and runs it, then throws the binary away
go build Compiles and writes an executable (tally, or tally.exe on Windows)
go fmt ./... Rewrites every file in the standard layout
go vet ./... Reports code that compiles but is probably a bug
go test ./... Runs the tests in _test.go files
go doc strings.Fields Prints the documentation for a package or function

./... means "this folder and every folder below it". A single . means just this folder.

A few more you will meet soon: go mod tidy adds missing dependencies to go.mod and removes unused ones; go install builds a program and puts it in your Go bin folder so you can run it by name; go env shows settings; go version shows the toolchain.

go run . notes.txt          # arguments after the package go to the program
go build -o bin/tally .     # choose the output name
GOOS=windows go build .     # cross-compile for Windows from any system

go vet deserves a special mention. This compiles fine but is wrong:

fmt.Printf("%d words\n", "seven")

go vet flags it because %d expects a number and gets a string. The compiler only checks that the call is legal; vet checks that it makes sense.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.