liveBook · Manning

State transition map

As explained in Build and Orchestrator in Go (From Scratch)

A state transition map associates each current task state with the states to which it may validly transition.

A state transition map makes task-state rules explicit. It associates each current state with the states a task may validly enter, preventing unsupported jumps such as Running to Scheduled or Failed to Scheduled. section 4.6.3

The implementation uses a map from State to []State. Pending allows Scheduled; Scheduled allows Running, Completed, or Failed; terminal states allow no further transitions. ValidStateTransition asks whether a destination appears in the current state's allowed slice, while Contains performs the lookup. section 4.6.3

Checking a transition

ValidStateTransition(Scheduled, Running) returns true because Running is listed for Scheduled. ValidStateTransition(Failed, Scheduled) returns false because Failed has no allowed destinations. section 4.6.3

Listing 4.10 The stateTransitionMap map
var stateTransitionMap = map[State][]State{
   Pending:   []State{Scheduled},
   Scheduled: []State{Scheduled, Running, Failed},
   Running:   []State{Running, Completed, Failed},
   Completed: []State{},
   Failed:    []State{},
}

Inspect the compact map that encodes each task state's permitted destinations. section 4.6.3

How Build and Orchestrator in Go (From Scratch) covers it

How deep it goes
Defines the map and helper logic, but stops short of a general state-machine implementation.
Examples
Uses Pending, Scheduled, Running, Completed, and Failed states with concrete transition checks.
What it assumes
Assumes basic task-state terminology while defining valid states, events, and transitions explicitly.
What you can do after
Build it from scratch as a Go map with Contains and ValidStateTransition helpers.
How it connects
Connects task states, events, state machines, and task orchestrators.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build and Orchestrator in Go (From Scratch). The section links point to the parts of the book it draws on.