Skip to content

Instantly share code, notes, and snippets.

@satriahrh
Created October 8, 2020 19:41
Show Gist options
  • Save satriahrh/9e1372f0b8d629647d3c66ee10d2e4a5 to your computer and use it in GitHub Desktop.
Save satriahrh/9e1372f0b8d629647d3c66ee10d2e4a5 to your computer and use it in GitHub Desktop.
type IFiniteStateMachine interface {
Current() State
Transition(event Event) error
}
func (fsm *FiniteStateMachine) Current() State {
if fsm.current == "" {
return fsm.Initial
}
return fsm.current
}
func (fsm *FiniteStateMachine) Transition(event Event) error {
action := fsm.StateMap[fsm.Current()][event]
if fmt.Sprint(action) != fmt.Sprint(Action{}) {
fsm.current = action.Destination
return nil
}
return fmt.Errorf("transition invalid")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment