Skip to content
GDP for Noobs
☰ All patterns
BehavioralIntermediate

Command

Turn actions into objects so you can queue, log and undo them.

01

Explain it like I'm new

Real-life analogy

A restaurant order ticket

The waiter writes your order on a ticket. It can be queued, handed to any cook, kept as a record, and torn up if you cancel.

😩 The problem

A text editor needs undo. If edits are just function calls that change text directly, there's no record of what happened or how to reverse it.

💡 The pattern

Wrap each action in an object with Execute() and Undo(). A history stack runs commands and pops them to undo.

02

Play with it

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

(empty document)

History stack (top = last command)

  • empty

💡 Each button creates a command object. Undo pops the top one and calls its Undo(). DeleteCmd remembered what it deleted!

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// Editor is the "receiver": the thing commands act on.
6type Editor struct{ text string }
7
8// Command wraps an action as an object, so it can be stored and undone.
9type Command interface {
10 Execute()
11 Undo()
12}
13
14type TypeCmd struct {
15 ed *Editor
16 text string
17}
18
19func (c *TypeCmd) Execute() { c.ed.text += c.text }
20func (c *TypeCmd) Undo() { c.ed.text = c.ed.text[:len(c.ed.text)-len(c.text)] }
21
22type DeleteCmd struct {
23 ed *Editor
24 n int
25 deleted string // remembered so Undo can restore it
26}
27
28func (c *DeleteCmd) Execute() {
29 cut := len(c.ed.text) - c.n
30 c.deleted = c.ed.text[cut:]
31 c.ed.text = c.ed.text[:cut]
32}
33func (c *DeleteCmd) Undo() { c.ed.text += c.deleted }
34
35// History is the "invoker": it runs commands and remembers them.
36type History struct{ done []Command }
37
38func (h *History) Run(c Command) {
39 c.Execute()
40 h.done = append(h.done, c)
41}
42
43func (h *History) Undo() {
44 if len(h.done) == 0 {
45 return
46 }
47 last := h.done[len(h.done)-1]
48 h.done = h.done[:len(h.done)-1]
49 last.Undo()
50}
51
52func main() {
53 ed := &Editor{}
54 h := &History{}
55 show := func(label string) { fmt.Printf("%-16s %q\n", label, ed.text) }
56
57 h.Run(&TypeCmd{ed: ed, text: "Hello"})
58 show("type Hello")
59 h.Run(&TypeCmd{ed: ed, text: ", World"})
60 show("type , World")
61 h.Run(&DeleteCmd{ed: ed, n: 5})
62 show("delete 5")
63 h.Undo()
64 show("undo")
65 h.Undo()
66 show("undo")
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…

  • •Undo/redo, job queues, macro recording, transactional operations.

🚫 Skip it when…

  • •Simple one-off calls with no need to store, delay or reverse them.

🐹 Go tip

For a job queue, send commands over a channel to worker goroutines. Command and worker pool fit together nicely.

🌍 You've already seen it in

exec.Cmd wraps a process to run later. Task queues like asynq and machinery store jobs as command objects.

05

Quick check

One question. No pressure.

Why does DeleteCmd store the `deleted` text?

Related: Strategy, Worker Pool