Simple & declarative finite state machines in Go
Visit https://pkg.go.dev/github.com/sebinbabu/zostate for API documentation.
package main
import (
"fmt"
"github.com/sebinbabu/zostate"
)
const (
On zostate.StateType = "On"
Off zostate.StateType = "Off"
)
const (
Toggle zostate.EventType = "Toggle"
)
func main() {
machine, err := zostate.NewMachine(
"LightBulb",
Off,
zostate.States{
{
Name: Off,
Transitions: zostate.Transitions{
{Event: Toggle, Dst: On},
},
},
{
Name: On,
Transitions: zostate.Transitions{
{Event: Toggle, Dst: Off},
},
},
},
)
fmt.Println("current state: ", machine.Current()) // current state: Off
machine.Transition(Toggle)
fmt.Println("current state: ", machine.Current()) // current state: On
}
- Add tests
- Add support for callbacks for events, states & transitions
- Implement statecharts
- Add mutexes to race avoid conditions.
- xstate: State machines and statecharts for the modern web
- looplab/fsm: Finite State Machine for Go