The go Command
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 systemgo 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.
The shortcut
Match the verb in your head to the subcommand: try it → run, ship it → build, tidy the look → fmt, sanity-check → vet, prove it → test, look it up → doc.
Try it
You changed the counting loop.
go run . notes.txtcompiles and runs in one step.
The common mix-up is go run versus go build. run is for trying; it leaves nothing behind. build is for keeping or sharing the program.
Drills
Answer fast — trust the rule.
You want to try your change right now and do not need a binary afterwards. Which command?
Which command catches fmt.Printf("%d", "seven") even though it compiles?
What does ./... mean in go test ./...?
You added an import of an outside package and deleted another. Which command updates go.mod to match?
What does GOOS=linux go build . produce on a Mac?
Remember
- One task, one subcommand: run, build, fmt, vet, test, doc.
- Shortcut: try → run, ship → build, look → fmt, sanity → vet, prove → test.
- Trap:
go runleaves no binary; vet catches bugs the compiler lets through.
