Package fsm allows you to add finite-state machines to your Go code.
States and Events are defined as int consts :
const (
StateFoo fsm.State = iota
StateBar
)
const (
EventFoo fsm.Event = iota
EventBar
)
f := fsm.New(StateFoo)
f.Transition(
fsm.On(EventFoo), fsm.Src(StateFoo),
fsm.Dst(StateBar),
)
You can have custom checks or actions :
f.Transition(
fsm.Src(StateFoo), fsm.Check(func () bool {
// check something
}),
fsm.Call(func () {
// do something
}),
)
Transitions can be triggered the second time an event occurs :
f.Transition(
fsm.On(EventFoo), fsm.Src(StateFoo), fsm.Times(2),
fsm.Dst(StateBar),
)
Functions call be called when entering or leaving a state :
f.EnterState(StateFoo, func() {
// do something
})
f.Enter(func(state fsm.State) {
// do something
})
f.ExitState(StateFoo, func() {
// do something
})
f.Exit(func(state fsm.State) {
// do something
})
go get github.com/cocoonspace/fsm
Contributions are welcome, as long as:
MIT - See LICENSE