Skip to content

Testing & Tooling

Most languages require a third-party testing framework. Go ships everything you need inside the standard library. The testing package provides *testing.T for unit tests, *testing.B for benchmarks, and *testing.F for fuzzing — all driven by a single command: go test.

The go toolchain that ships with every Go installation also includes:

ToolPurpose
go testRun tests and benchmarks
go buildCompile packages and binaries
go runCompile and run in one step
go fmtFormat source code (wraps gofmt)
go vetStatic analysis for common mistakes
go modModule and dependency management
go docBrowse package documentation
  1. go-test — Writing _test.go files, func TestXxx(t *testing.T), and table-driven tests with t.Run.
  2. benchmarks — Measuring performance with func BenchmarkXxx(b *testing.B) and reading -benchmem output.
  3. modules — Managing dependencies with go mod init, go.mod, go.sum, and go get.
  4. go-tooling — The everyday commands: go run, go build, go fmt, go vet, and go doc.

The best way to appreciate Go’s testing tools is to start with a pure function — one with no side effects, no I/O, just input and output. The playground below runs Fibonacci from main. In the next lesson you will write a proper _test.go file for exactly this kind of function.

package main
import "fmt"
// Fibonacci returns the nth Fibonacci number (0-indexed).
// fib(0)=0, fib(1)=1, fib(2)=1, fib(3)=2, ...
func Fibonacci(n int) int {
if n <= 1 {
return n
}
return Fibonacci(n-1) + Fibonacci(n-2)
}
// IsPrime reports whether n is a prime number.
func IsPrime(n int) bool {
if n < 2 {
return false
}
for i := 2; i*i <= n; i++ {
if n%i == 0 {
return false
}
}
return true
}
func main() {
// Print the first 10 Fibonacci numbers
fmt.Println("Fibonacci sequence:")
for i := 0; i < 10; i++ {
fmt.Printf(" fib(%d) = %d\n", i, Fibonacci(i))
}
// Find primes up to 30
fmt.Println("Primes up to 30:")
for n := 2; n <= 30; n++ {
if IsPrime(n) {
fmt.Printf(" %d\n", n)
}
}
}
Which command runs all tests in the current module and every sub-package?
Which Go standard library package provides *testing.T and *testing.B?
What makes a function a good candidate for a first unit test?
Which tool formats Go source code according to the canonical style?