fsmgo is a Go library for building finite state machines in a clear, modular, and reusable way. It's inspired by FSMgasm, a Kotlin library originally designed for Minecraft minigames, but fsmgo has been adapted with Go idioms to fit naturally into concurrent architectures and complex systems.
go get github.com/josscoder/fsmgo
Import the package into your code:
import "github.com/josscoder/fsmgo/state"
fsmgo revolves around the definition of states and their composition. A state has a lifecycle with three main phases:
-
OnStart()
-
OnUpdate()
-
OnEnd()
Additionally, states can implement optional lifecycle hooks for pause and resume:
-
OnPause()
-
OnResume()
These two methods are not required as part of the main State interface. Instead, they belong to an optional interface named PauseAware:
type PauseAware interface {
OnPause()
OnResume()
}
When a state implements PauseAware, the FSM system will automatically call OnPause()
and OnResume()
when appropriate. States that don't require pause/resume behavior can ignore this interface entirely.
Each state can also have a duration or a custom end condition to determine when it completes.
Explore how to use fsmgo in the example directory.
fsmgo is licensed under the MIT License. Feel free to use, modify, and distribute it in your projects.