Table of Contents
Month 4 · Week 3 — Exercises (production handler concerns)¶
Standard-library-only Go packages with table-driven tests. Each exercise isolates
one cross-cutting concern of a real HTTP service — validation, pagination,
configuration — as pure, testable logic, so go test runs anywhere with no
server or environment setup.
How to run¶
go test ./exercises/month-04/week-3/...
# or one at a time, verbose:
go test -v ./exercises/month-04/week-3/validate
Prompts¶
-
validate/— Implement request validation that collects every field error at once into aFieldErrorsmap (first-error-per-field wins). Rules forNewUser:Namerequired + trimmed + ≤ 50 runes,Emailparses vianet/mail.ParseAddress,Agein 18..130. AddOK()and a sortedFields()for deterministic output. Concepts: decode-then-validate, 400 vs 422, fail-collect (Day 099). -
paginate/— ImplementParse(url.Values) Pagethat readslimit/offsetand clamps rather than errors: garbage/missing/<1limit →DefaultLimit,>MaxLimit→MaxLimit, negative offset → 0. ThenSlice(total)/SliceStringsmust be bounds-safe — an offset past the end yields an empty (non-nil) slice, never a panic. Concepts: hostile query params, clamping, bounds-safe slicing (Day 101). -
envconfig/— ImplementLoad(lookup LookupFunc) (Config, error)that builds a typedConfigfrom an injected env lookup: defaults forPORT/TIMEOUT/DEBUG, a required non-emptyDATABASE_URL, typed parsing (strconv/time.ParseDuration), and all problems accumulated viaerrors.Join.MapLookupadapts a map for tests. Concepts:os.LookupEnvsemantics, fail-fast config, DI (Day 102).
Results¶
| Exercise | Focus | Tests | Status |
|---|---|---|---|
validate |
collected field errors, 400 vs 422 | go test |
✅ |
paginate |
clamped params + bounds-safe slicing | go test |
✅ |
envconfig |
typed env config + joined errors | go test |
✅ |
Idiom checklist exercised this week:
net/mail.ParseAddressover regex,min-clamped slice bounds,os.LookupEnv's(value, ok),errors.Joinfor fail-fast config, injected dependencies (now,LookupFunc) for pure tests, and table-driven tests withreflect.DeepEqual.Rate limiting (Day 103) and JWT/RBAC (Day 104) live as runnable programs in
examples/month-04/ratelimitandexamples/month-04/jwtrather than as exercises here.