liveBook · Manning

Go command

As explained in Go in Practice, Second Edition

The go tool is Go's command-line tool for compiling and performing other standard development tasks.

The `go` command is Go’s command-line toolchain. It compiles and runs packages, but also brings package management, formatting, testing, and documentation into the same development workflow. section 1.1 section 6.0

Use its commands as the task changes:

  • Manage modules and dependencies with `go mod` and `go get ./...`; use `go fmt` to keep code formatting consistent. section 1.2.4
  • Run unit tests with `go test`, then use `go test -cover` to measure statement coverage. section 1.2.4
  • Use the toolchain’s documentation and code-checking capabilities alongside building and testing; `vet` is among the available tools. section 1.2.4 section 6.0

Test a change before shipping it

A name-reversal test fails under `go test`. Changing the reversal to work with runes rather than bytes makes the test pass; `go test -cover` then reports how many statements the tests exercised. The failure, fix, and coverage result are separate checks: passing tests do not imply comprehensive coverage. section 1.2.4

For a binary targeting another platform, set `GOOS` and `GOARCH` when building. Test the result on its target platform, especially if the application uses cgo; cross-compilation can introduce complications with C libraries. section 12.4.2

$ GOOS=windows GOARCH=386 go build

This command builds a Windows binary for the 386 architecture by setting the target operating system and architecture for `go build`. section 12.4.2

How Go in Practice, Second Edition covers it

How deep it goes
Shows task-level commands, test output, coverage, and cross-compilation settings; does not descend into compiler internals.
Examples
Uses a failing name-reversal test, a coverage report, and a Windows 386 build command.
What it assumes
Assumes a developer ready to work with Go applications while introducing the accompanying toolchain from basic tasks.
What you can do after
Operate/configure it to test and measure Go code, manage dependencies, format code, and build for another platform.
How it connects
Connects Go modules and packages with formatting, unit testing, code coverage, documentation, and cross-compilation.

Also explained in

Compare how 2 books explain go command →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go in Practice, Second Edition. The section links point to the parts of the book it draws on.