Skip to content
GDP for Noobs
☰ All patterns
BehavioralIntermediate

State

Let an object change its behaviour when its internal state changes.

01

Explain it like I'm new

Real-life analogy

A traffic light

The same light behaves differently depending on its colour: red means walk, green means wait. Each colour knows which colour comes next.

😩 The problem

Every method has a switch l.status { case red...; case green... }. Adding a state means editing every switch, and invalid transitions slip through.

💡 The pattern

Make each state its own type implementing a State interface. The context delegates to its current state, and states decide the next state themselves.

02

Play with it

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

current state

Red{}

state.CanCross()

true 🚶

state = Red{}

💡 Notice there's no switch anywhere. Each state type knows its own behaviour and which state comes next.

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
5// State describes how the light behaves in ONE particular state.
6type State interface {
7 Name() string
8 CanCross() bool
9 Next(l *TrafficLight)
10}
11
12type Red struct{}
13type Green struct{}
14type Yellow struct{}
15
16// Each state decides what the NEXT state is. No giant switch needed.
17func (Red) Name() string { return "RED" }
18func (Red) CanCross() bool { return true }
19func (Red) Next(l *TrafficLight) { l.state = Green{} }
20func (Green) Name() string { return "GREEN" }
21func (Green) CanCross() bool { return false }
22func (Green) Next(l *TrafficLight) { l.state = Yellow{} }
23func (Yellow) Name() string { return "YELLOW" }
24func (Yellow) CanCross() bool { return false }
25func (Yellow) Next(l *TrafficLight) { l.state = Red{} }
26
27// TrafficLight (the "context") just delegates to its current state.
28type TrafficLight struct{ state State }
29
30func (l *TrafficLight) Tick() { l.state.Next(l) }
31
32func main() {
33 light := &TrafficLight{state: Red{}}
34 for i := 0; i < 5; i++ {
35 walk := "wait"
36 if light.state.CanCross() {
37 walk = "walk"
38 }
39 fmt.Printf("light is %-6s -> pedestrians %s\n", light.state.Name(), walk)
40 light.Tick()
41 }
42}
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…

  • •Workflows: orders, payments, connections, game characters, UI modes.

🚫 Skip it when…

  • •Only 2 states with trivial logic. A boolean or small switch is fine.

🐹 Go tip

For simple machines, a map[State][]State of allowed transitions plus a switch is also very idiomatic Go.

🌍 You've already seen it in

TCP connection states, the text/template lexer (states as functions) and order workflows in e-commerce backends.

05

Quick check

One question. No pressure.

In the State pattern, who decides what the next state is?

Related: Strategy, Command