Decorator Pattern Implementation in Go
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓Decorator Pattern runs in O(1) time and O(n) space, in Go as in every other language.
- ✓Struct embedding gives you the pattern with the least ceremony of any language here: embed an interface value in a struct and every method of that interface is PROMOTED to the outer type automatically, so you write only the methods you actually want to change.
- ✓Embed the INTERFACE, not a concrete struct, so your decorator accepts any implementation. Override only the methods whose behaviour changes and let promotion forward the rest.
- ✓The Go trap: Embedding an interface whose value is NIL compiles perfectly and panics at runtime the moment an un-overridden promoted method is called, with a nil-pointer dereference pointing at a line you never wrote, the cost of the convenience.
- ✓Promoted method calls through an embedded interface are ordinary interface calls, one indirect jump, no allocation, and the compiler can devirtualise and inline them when the concrete type is known at the call site.
What Decorator Pattern actually is
Decorator Pattern in Go: what you actually reach for
Go implementation
package store
import (
"log"
"net/http"
"time"
)
type Store interface {
Get(key string) (string, bool)
Set(key, value string)
}
// Embedding the INTERFACE promotes every method: override only Get.
type Timed struct {
Store // nil here compiles and panics at the first promoted call
}
func (t Timed) Get(key string) (string, bool) {
start := time.Now()
v, ok := t.Store.Get(key)
log.Printf("get %s took %s", key, time.Since(start))
return v, ok
}
// Set is NOT written here, promotion forwards it automatically.
// The canonical Go middleware shape:
func WithLogging(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
next.ServeHTTP(w, r)
log.Printf("%s %s %s", r.Method, r.URL.Path, time.Since(start))
})
}
// Promotion is NOT virtual dispatch: if the embedded Get called Set
// internally, it would call the EMBEDDED Set, never your override.
// Go has no super and no back-dispatch to the outer type.Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe Go-specific trap
Performance in Go, beyond the O(1)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which Go type or library should I use for Decorator Pattern?
Struct embedding gives you the pattern with the least ceremony of any language here: embed an interface value in a struct and every method of that interface is PROMOTED to the outer type automatically, so you write only the methods you actually want to change. The standard library uses this everywhere, bufio.Reader wraps io.Reader, and httptest and middleware chains follow the same shape. http.Handler decoration (a func taking a Handler and returning a Handler) is the canonical Go middleware idiom.
What is the most common Go mistake when implementing Decorator Pattern?
Embedding an interface whose value is NIL compiles perfectly and panics at runtime the moment an un-overridden promoted method is called, with a nil-pointer dereference pointing at a line you never wrote, the cost of the convenience. Second, promotion is NOT virtual dispatch: if a promoted method on the embedded value calls another method of the interface, it calls the EMBEDDED implementation, not your override, because Go has no super and no dynamic dispatch back to the outer type. Wrapping http.ResponseWriter has a specific version of this: your wrapper silently drops the optional http.Flusher and http.Hijacker interfaces unless you re-implement them.
How does Decorator Pattern actually perform in Go?
Promoted method calls through an embedded interface are ordinary interface calls, one indirect jump, no allocation, and the compiler can devirtualise and inline them when the concrete type is known at the call site. Embedding a concrete struct instead of an interface gives fully static dispatch and is free. A decorator chain costs one interface call per layer, which is negligible next to any I/O the chain is wrapping. Time complexity is O(1) and space is O(n).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialIf you are a student, see what GitHub Copilot offers students for free on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.