Created
October 8, 2020 19:41
-
-
Save satriahrh/9e1372f0b8d629647d3c66ee10d2e4a5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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