Module 15 Β· Design Patterns in Go β Lesson 5 of 8 Β· ~6 min
Decorator / Middleware
Wrap behavior around something you can't change
Decorator wraps an object in another that shares its interface and adds behavior β logging, timing, auth β without touching the wrapped thing. You already met the Go form of this in module 06: HTTP middleware. A middleware takes a handler and returns a handler.
The signature is the whole pattern: func(next http.Handler) http.Handler. The wrapper does its extra work, then calls next.ServeHTTP to delegate. Because the return type matches the input type, decorators stack β Logging(Auth(handler)) is a handler that logs, then authorizes, then runs the real one.