A simple Go project for finite state machines (FSM). I got inspiration by my wife Kry. I really love her!
Thank you for using my code. Kry will be very happy if you like it.
kry is a Go-based library for building and running finite state machines (FSM) in your applications. It provides a simple API to define states, transitions, and handle events.
- Install the package:
go get github.com/rianby64/kry- Import and use in your Go code:
package main
import (
"context"
"fmt"
"github.com/rianby64/kry"
)
type CustomParam struct {
Value string
}
func main() {
const (
initial int = iota
close
open
)
ctx := context.TODO()
fsk, err := kry.New(initial, []kry.Transition[string, int, CustomParam]{
{
Name: "open",
Src: []int{initial, close},
Dst: open,
Enter: func(ctx context.Context, instance kry.InstanceFSM[string, int, CustomParam], param CustomParam) error {
fmt.Println("Opened with param:", param.Value)
return nil
},
},
{
Name: "close",
Src: []int{open},
Dst: close,
},
}, kry.WithFullHistory())
if err != nil {
panic(err)
}
if err := fsk.Event(ctx, "open", CustomParam{Value: "example"}); err != nil {
panic(err)
}
fmt.Println("Current state:", fsk.Current())
if err := fsk.Apply(ctx, "close", close); err != nil {
panic(err)
}
fmt.Println("Current state:", fsk.Current())
}- Go 1.23 or higher
- Simple API
- Support for source transitions matching via function. (All 5xx, 4xx, etc.)
- Support for destination transitions matching via function. (All 5xx, 4xx, etc.)
- Visualization tools for FSMs
- From transition - do a call to another transition, and do not allow looping
- History of transitions with safe for concurrent use
- Add more examples and documentation
- Implement more advanced features like state beforeEnter/exit actions. (should I?)
- Improve the history with a better flow. At this moment it's basic.
- This library does not support
ForceStateas it is dangerous and breaks the FSM concept. So, if you encounter such a need, please rethink your design. (Experimenting...) - Keep the API simple and easy to use.
- It's up to you to ensure that the FSM is used in a thread-safe manner if needed, so lock it in the callbacks Enter* if you need to.
This project is licensed under the MIT License.