☰ All patterns
Chain of Responsibility
Pass a request along a chain until someone handles or rejects it.
01
Explain it like I'm new
Real-life analogy
Airport security
Ticket check → passport control → bag scan → gate. Each checkpoint can stop you or wave you on to the next one.
😩 The problem
Each request needs auth, rate limiting and validation. One giant function doing all three is hard to reorder, reuse or test.
💡 The pattern
Each check is a handler with a link to the next one. A handler either stops with an error or passes the request on.
02
Walk through the code
Click a step to highlight its lines. This is a complete program you can copy and run.
Press ▶ Run to execute this program.This output was captured by running the program with Go 1.24. Copy it into your editor or the Go Playground and try changing things.
03
When should I use it?
✅ Reach for it when…
- •Middleware, validation pipelines, approval workflows, event handling with fallbacks.
🚫 Skip it when…
- •The order of checks never changes and there are only one or two. Inline them.
🐹 Go tip
Struct embedding (struct{ BaseHandler }) is Go's way of sharing code. It's composition, not inheritance.
🌍 You've already seen it in
HTTP middleware stacks in chi, echo and gin, and gRPC interceptor chains.
04
Quick check
One question. No pressure.
Request 2 has a wrong token. Which handlers run?