Skip to content

Instantly share code, notes, and snippets.

@dipeshdulal
Last active October 9, 2020 03:05
Show Gist options
  • Save dipeshdulal/f41f05d3ef69dc2f5795b0fd626058ea to your computer and use it in GitHub Desktop.
Save dipeshdulal/f41f05d3ef69dc2f5795b0fd626058ea to your computer and use it in GitHub Desktop.
State Machine Transition Engine
// IMachine machine interface
type IMachine interface {
Transition() string
Current() string
}
// Current returns current state
func (m *Machine) Current() string {
if m.current == "" {
return m.Initial
}
return m.current
}
// Transition transitions to next state
func (m *Machine) Transition(event string) string {
current := m.Current()
transitions := m.States[current].On
next := transitions[event].To
if next != "" {
m.current = next
return next
}
return current
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment