Skip to content
GDP for Noobs
☰ All patterns
BehavioralIntermediate

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.

EventBus
📧

EmailAlert

waiting…

📒

AuditLog

waiting…

📊

Dashboard

waiting…

💬

SlackBot

not listening

Publish an event, then unsubscribe someone and publish again.

💡 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.

main.go
1package main
2
3import "fmt"
4
5type Event struct {
6 Topic string
7 Data string
8}
9
10// Subscriber is anyone who wants to hear about events.
11type Subscriber interface {
12 Notify(e Event)
13}
14
15type EmailAlert struct{ addr string }
16type AuditLog struct{}
17type Dashboard struct{ count int }
18
19func (e *EmailAlert) Notify(ev Event) { fmt.Printf(" email to %s: %s\n", e.addr, ev.Data) }
20func (a *AuditLog) Notify(ev Event) { fmt.Printf(" audit: [%s] %s\n", ev.Topic, ev.Data) }
21func (d *Dashboard) Notify(ev Event) {
22 d.count++
23 fmt.Printf(" dashboard: %d orders so far\n", d.count)
24}
25
26// EventBus keeps a list of subscribers per topic.
27type EventBus struct {
28 subs map[string][]Subscriber
29}
30
31func NewEventBus() *EventBus { return &EventBus{subs: map[string][]Subscriber{}} }
32
33func (b *EventBus) Subscribe(topic string, s Subscriber) {
34 b.subs[topic] = append(b.subs[topic], s)
35}
36
37func (b *EventBus) Unsubscribe(topic string, s Subscriber) {
38 list := b.subs[topic]
39 for i, existing := range list {
40 if existing == s {
41 b.subs[topic] = append(list[:i], list[i+1:]...)
42 return
43 }
44 }
45}
46
47// Publish tells EVERY subscriber. The publisher doesn't know who they are.
48func (b *EventBus) Publish(e Event) {
49 for _, s := range b.subs[e.Topic] {
50 s.Notify(e)
51 }
52}
53
54func main() {
55 bus := NewEventBus()
56 email := &EmailAlert{addr: "boss@shop.io"}
57 bus.Subscribe("order.created", email)
58 bus.Subscribe("order.created", &AuditLog{})
59 bus.Subscribe("order.created", &Dashboard{})
60
61 fmt.Println("Publishing order #1")
62 bus.Publish(Event{"order.created", "order #1 for $20"})
63
64 bus.Unsubscribe("order.created", email)
65 fmt.Println("Publishing order #2 (email unsubscribed)")
66 bus.Publish(Event{"order.created", "order #2 for $35"})
67}
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…

  • •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