Statecharts for Go · v1.4.1

Stop modeling order, payment, and incident lifecycleswith switch statements.

Statekit is a typed statechart library for Go: hierarchical states, guards, actions, delayed and parallel transitions, with built-in visualization and lint. The same primitives serve backend domain workflows of every shape.

# Install
$ go get go.klarlabs.de/statekit

// A working machine in 10 lines
machine, _ := statekit.NewMachine[Order]("checkout").
    WithInitial("cart").
    State("cart").On("CHECKOUT").Target("processing").Done().
    State("processing").On("PAID").Target("shipped").Done().
    State("shipped").Final().Done().
    Build()

interp := statekit.NewInterpreter(machine)
defer interp.Close()
interp.Start()
interp.Send(statekit.Event{Type: "CHECKOUT"})

Why Statekit

Type-safe over typed

[C any] context, typed events, typed actions and guards. No interface{} casts at handler time.

Statecharts, not just FSMs

Compound, parallel, history, and delayed transitions handle the workflows that flat FSM libraries can't model without manual bookkeeping.

Visualization as a feature

Every machine renders to ASCII, Mermaid, interactive HTML, or TUI from one source. XState v5 export for Stately Studio round-trip editing.

Static analysis

lint.Lint(machine) catches unreachable states, dead ends, non-determinism, missing OnError on Invoke — at build time.

Determinism for tests

Inject a FakeClock to make timer-driven behavior reproducible. No time.Sleep flakes.

Built for backend workflows

Order lifecycles, payment sagas, incident management, KYC (Stripe webhook saga template included) all share the same typed primitives.

Coming from another library?

Concernlooplab/fsmqmuntal/statelessStatekit
Generics / typed contextopen issueopen issuefirst-class
Hierarchical statespartial (substates)compound + parallel + history
Persistence (gob/JSON)#40 unresolvedmanualSnapshot round-trips
Cancellation recovery#115 stuckOnError routing
Runaway prevention#77 manualGuard + attempt counter
VisualizationDOTDOTASCII / Mermaid / HTML / TUI
Static analysislint with 9 rules

Built for backend workflows

Backend domain workflows

Order lifecycles, payment sagas, incident management, KYC. The kind of state most teams scatter across switch event.Type and accumulate bugs around partial failure, retry, and idempotency.

See the Stripe webhook saga →

Reproducible by construction

Event sourcing replays a run deterministically, OnError routing bounds the blast radius, and snapshots give you audit-grade observability — the same typed primitives across every workflow.

See the incident lifecycle example →

Ready?

Drop it into your service. Or paste any Native JSON into the visualizer first.