Middleware
Logging, auth, header-tagging, panic recovery — every request passes through concerns that aren’t any single handler’s business. Duplicating them into each handler rots within weeks. Middleware factors them out: handlers wrapped in handlers, each layer adding behavior around the next.
Handlers all the way down
// Syntax: a function from handlers to handlers
func WithHeader(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Course", "go")
next.ServeHTTP(w, r)
})
}The shape func(http.Handler) http.Handler is the whole pattern. Take the
next handler, return a new one that does its work and delegates. The
returned closure is a handler (via the HandlerFunc adapter from the
first lesson), so it chains arbitrarily:
WithHeader(logging(mux))Read inside out: requests hit WithHeader first, then logging, then the
mux. Each layer runs its pre-work, calls next, and optionally does
post-work after it returns. Forget the next call and the chain stops
dead — requests answered (or more often, unanswered) mid-stack. A
middleware that never delegates isn’t layered behavior; it’s a wall.
Headers before delegation
This exercise’s layer tags every response:
w.Header().Set("X-Course", "go")
next.ServeHTTP(w, r)Set-then-delegate, in that order — headers mutate freely before the inner
handler writes, and the freezing law (status lesson) still governs the
final bytes. The test asserts both halves independently: the header exists
and the inner body "ok" survived, proving the layer added without
obstructing. A middleware test always checks both directions — what it
changed, and what it preserved.
flowchart TD
R[Request] --> H[WithHeader: set X-Course]
H --> L[logging: pre-work]
L --> M[mux: route by pattern]
M --> H2[handler: write response]
H2 --> L2[logging: post-work]
L2 --> H3[WithHeader: post-work]
H3 --> Resp[Response]
Hint
Set the header with w.Header().Set before calling
next.ServeHTTP(w, r). Both halves matter: the tag added, the chain
continued.
Task
Fill in WithHeader in mw.go:
func WithHeader(next http.Handler) http.Handler {
// ...
}Set the X-Course: go response header before delegating to next. The stub
delegates without setting it, so the test fails on the header.
Check
go test ./exercises/13_http/07_middleware/ -vSource: exercises/13_http/07_middleware/README.md · Inspired by Mainmatter’s 100 Exercises to Learn Rust (CC BY-NC 4.0) — all Go prose here is original, free for non-commercial use.