☰ All patterns
Observer
Notify many subscribers automatically when something happens.
01
Explain it like I'm new
Real-life analogy
Subscribing to a YouTube channel
You hit subscribe once. Whenever a new video drops, every subscriber gets notified. The creator doesn't know who you are, and you can unsubscribe anytime.
😩 The problem
When an order is created, email, audit logging and the dashboard all need to react. Hard-coding those calls couples order code to every feature.
💡 The pattern
An event bus keeps a list of subscribers per topic. Publishers call Publish; the bus calls Notify on everyone subscribed.
02
Play with it
An interactive model of the pattern. Click around, nothing can break.
EmailAlert
waiting…
AuditLog
waiting…
Dashboard
waiting…
SlackBot
not listening
💡 The publisher never changes when you add SlackBot. That's loose coupling.
03
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.
04
When should I use it?
✅ Reach for it when…
- •Event-driven features, plugins, UI updates, domain events.
🚫 Skip it when…
- •Event chains make the flow hard to follow. Prefer direct calls for simple cases.
🐹 Go tip
In concurrent code, protect the subscriber map with a sync.RWMutex, or deliver events through channels so slow subscribers don't block publishers.
🌍 You've already seen it in
signal.Notify(ch, os.Interrupt) subscribes a channel to OS signals. Event buses like NATS clients follow the same idea.
05
Quick check
One question. No pressure.
What does the publisher need to know about its subscribers?
Related: Command, Fan-out / Fan-in