Skip to content
GDP for Noobs
☰ All patterns
StructuralIntermediate

Decorator

Wrap behaviour in layers to add features without changing it.

01

Explain it like I'm new

Real-life analogy

Gift wrapping

The gift stays the same. You add wrapping paper, then a ribbon, then a card. Each layer adds something, and you can add or remove layers freely.

😩 The problem

You want logging, uppercasing and excitement on top of a function, in different combinations, without editing the original or writing a subclass for every combo.

πŸ’‘ The pattern

A decorator takes a function (or interface) and returns a new one with the same signature that adds behaviour before or after calling the original.

02

Play with it

An interactive model of the pattern. Click around, nothing can break.

Layers (outermost first). Toggle and reorder them:

  • WithLogging
  • WithUppercase
  • WithExcitement
  • WithTruncate(10)

The onion

WithLogging
WithUppercase
WithExcitement
Hello

WithLogging(WithUppercase(WithExcitement(Hello)))("gopher")

[log] got "HELLO, GOPHER!!!"
β†’ "HELLO, GOPHER!!!"

πŸ’‘ Turn on WithTruncate, then move it above and below WithExcitement. Order changes the result, just like the order of HTTP middleware matters.

03

Walk through the code

Click a step to highlight its lines. This is a complete program you can copy and run.

main.go
1package main
2
3import (
4 "fmt"
5 "strings"
6)
7
8// Greeter is the core behaviour we want to decorate.
9type Greeter func(name string) string
10
11func Hello(name string) string { return "hello, " + name }
12
13// A decorator takes a Greeter and returns a NEW Greeter that adds something.
14func WithLogging(next Greeter) Greeter {
15 return func(name string) string {
16 fmt.Printf("[log] -> called with %q\n", name)
17 out := next(name)
18 fmt.Printf("[log] <- returned %q\n", out)
19 return out
20 }
21}
22
23func WithUppercase(next Greeter) Greeter {
24 return func(name string) string { return strings.ToUpper(next(name)) }
25}
26
27func WithExcitement(next Greeter) Greeter {
28 return func(name string) string { return next(name) + "!!!" }
29}
30
31func main() {
32 plain := Greeter(Hello)
33 fmt.Println("plain:", plain("gopher"))
34
35 // Stack decorators like layers of an onion. Innermost runs first.
36 fancy := WithLogging(WithUppercase(WithExcitement(Hello)))
37 fmt.Println("fancy:", fancy("gopher"))
38}
terminal
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.

04

When should I use it?

βœ… Reach for it when…

  • β€’Cross-cutting concerns: logging, metrics, retries, auth, caching.
  • β€’HTTP middleware chains.

🚫 Skip it when…

  • β€’Very deep stacks become hard to debug. Keep chains short and named.

🐹 Go tip

This is exactly how net/http middleware works: func Logging(next http.Handler) http.Handler.

🌍 You've already seen it in

net/http middleware, bufio.NewReader(r) wrapping an io.Reader, and gzip.NewWriter(w).

05

Quick check

One question. No pressure.

In WithLogging(WithUppercase(Hello)), which function produces the original string first?

Related: Proxy, Chain of Responsibility, Adapter