Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
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.

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 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.

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.

One session on tallyStep 1 of 5
  1. Try it

    You changed the counting loop. go run . notes.txt compiles 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.

quick drill

You want to try your change right now and do not need a binary afterwards. Which command?

quick drill

Which command catches fmt.Printf("%d", "seven") even though it compiles?

quick drill

What does ./... mean in go test ./...?

quick drill

You added an import of an outside package and deleted another. Which command updates go.mod to match?

quick drill

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 run leaves no binary; vet catches bugs the compiler lets through.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainA day with tally

Describe a short working session on tally: you edit main.go, want to try it, tidy its layout, check it for suspicious code, run its tests and finally hand a colleague an executable. Name the go subcommand for each step and say what it does.