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