Weekly Review — Month 2 · Week 3 (Days 043–049)¶
📅 The Week in One Line¶
Learned to prove and measure Go code with the standard testing package: table-driven t.Run subtests, parallelism + helpers, httptest for handlers, coverage profiling, testing.B benchmarks, and testing.F fuzzing with Example docs.
✅ What I Completed¶
- Day 043 — Table-driven tests & subtests (
reverse,wordfreqexercises) - Day 044 —
t.Parallel,t.Helper,t.Cleanup, the loop-var capture trap - Day 045 —
httptestrecorder vs server (+httptest-demoexample,greeterexercise) - Day 046 — Coverage (
-cover,go tool cover) & wheretestifyfits - Day 047 — Benchmarks:
b.N,b.ResetTimer,b.ReportAllocs(+bench-demoexample) - Day 048 — Fuzzing (
testing.F) &Examplefunctions (+fuzz-roundtripexample) - Day 049 — Review & closed-book recall
- Runnable examples: 3 (
httptest-demo,bench-demo,fuzz-roundtrip) - Exercises solved: 3 (
reverse,wordfreq,greeter) — table tests + fuzz + benchmark
💡 Lessons Learned¶
- The table-driven +
t.Runpattern is the backbone: named rows give per-case failures andgo test -run TestX/casetargeting. t.Errorcollects all failures;t.Fatalstops the current (sub)test only — never call it from a spawned goroutine, it won't fail the test.t.Parallelpauses a subtest until the parent's serial part returns, then runs siblings together; pair withtc := tcto be capture-safe on every Go version.httptest.NewRecordertests handlers in-process with no socket; reserveNewServerfor real client/transport/TLS round trips.- Coverage is statement coverage — 100% is not correctness; chase error branches, not green lines. Use
-covermode=atomicwith-race. - A benchmark can lie two ways: timing setup (fix with
b.ResetTimer) and dead-code elimination (sink the result).-benchmem/b.ReportAllocsoften matters more thanns/op. - Fuzzing asserts invariants (round-trip, idempotence, never-panic, stays-valid) over generated inputs; plain
go testruns only seeds,-fuzzmutates and saves failing inputs totestdata. Examplefunctions with// Output:are tests and rendered docs on pkg.go.dev.
💪 Strengths (what clicked)¶
- Writing table-driven subtests fast and reading got-vs-want failures without an assertion lib.
httptest.NewRecordermechanics — callingServeHTTPdirectly felt natural.- Spotting the UTF-8 bug a byte-reverse hides, and expressing it as a fuzz invariant.
🧩 Weaknesses (what's still fuzzy)¶
- Interpreting noisy benchmark numbers and using
benchstatconfidently (need ≥10-countruns). - Choosing
-coverpkgboundaries for cross-package coverage credit. - Minimization/shrinking internals of the fuzz engine.
🔁 Spaced-Repetition Re-quiz (topics from earlier weeks)¶
- Q: (Month 1) Nil-interface trap: when is an interface holding a nil pointer
!= nil?A
An interface value is nil only when both its type and value are nil. A non-nil concrete type with a nil pointer value makes the interface non-nil — the classicreturn errwhereerris a typed-nil*MyError. - Q: (Month 2 W1) Why must you
Flushabufio.Writer?A
It buffers writes in memory; withoutFlushthe pending bytes never reach the underlying writer — silent data loss. - Q: (Month 2 W1)
string(65)— what do you get and what should you use?A
"A"(rune→string conversion), not"65". Usestrconv.Itoa/FormatIntfor number→text. - Q: (This week) Who sets
b.Nin a benchmark?A
The framework auto-tunes it upward until the loop runs ~1s, then reports total/b.Nas ns/op. - Q: (This week) What does plain
go testdo withFuzzXxx?A
Runs it against the seed corpus only (deterministic);-fuzz=FuzzXxxenters the mutation engine.
🎯 Action Items¶
- Run
go test -bench=. -benchmem -count=10 ./exercises/month-02/week-3/reverseand compare withbenchstat. - Add an
ExampleReverseand anExampleReverse_unicodeto thereversepackage. - Fuzz
reversefor 30s (-fuzz=FuzzReverse -fuzztime=30s) and commit anytestdataseed it finds. - Generate an HTML coverage report for
week-3and look for uncovered error branches.
🚀 Next Week Goals¶
- Profiling with
pprof(CPU/heap) to explain why a benchmark differs. - Static analysis:
go vet,staticcheck, andgo test -racein CI. - Modules & versioning:
go.mod, semantic import versioning,go mod tidy. - Build & release tooling:
go buildflags, build tags, cross-compilation.
📊 Metrics¶
| Hours | Days hit | Exercises | Commits | Avg confidence |
|---|---|---|---|---|
| 10.5 | 7/7 | 3 | 7 | 3.⅗ |
Suggested commit: docs(journal): month 2 week 3 review