Stage 1 · Code
Testing & Debugging in Go
Table-Driven Tests
Structure test suites as data — input structs, expected values, and named cases — for maximum coverage with minimum code.
The Table Pattern
A table-driven test defines every scenario as a row in a data structure. Adding a new case means adding one row — no new test function, no duplicated setup code.
Testing Error Paths
Every if err != nil in your code is a branch that should have a test. Use table cases with wantErr: true to test all failure paths.
Golden Files
A golden file stores the expected output of a function. Instead of embedding a long expected string in the test, compare against a file that can be regenerated with -update.
Test Fixtures
Test fixtures are reusable setup and teardown helpers. Extract repeated setup into a helper function that returns the constructed object and a cleanup function.
The real value of a test suite is not the initial green run. It is the confidence to rename a function, restructure a package, or replace a dependency — and know within seconds whether anything broke.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.