in

Finite State Machine package in Go


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 can 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	
})

https://github.com/cocoonspace/fsm-bench

Installation

go get github.com/cocoonspace/fsm

View Github




Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

GIPHY App Key not set. Please check settings

Make My Meal App Built Using Flutter

Explaining Like I’m Five: Connecting Front and Backend Systems