Skip to content

Josscoder/fsmgo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

fsmgo logo

Finite State Machine for Go

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.

Installation

go get github.com/josscoder/fsmgo

Import the package into your code:

import "github.com/josscoder/fsmgo/state"

Concepts

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.

Examples

Explore how to use fsmgo in the example directory.

License

fsmgo is licensed under the MIT License. Feel free to use, modify, and distribute it in your projects.

About

Finite State Machine for Go

Topics

Resources

License

Stars

Watchers

Forks

Languages