Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/looplab/fsm
Finite State Machine for Go
https://github.com/looplab/fsm
finite-state-machine fsm go
Last synced: 11 days ago
JSON representation
Finite State Machine for Go
- Host: GitHub
- URL: https://github.com/looplab/fsm
- Owner: looplab
- License: apache-2.0
- Created: 2013-08-21T14:30:43.000Z (about 11 years ago)
- Default Branch: main
- Last Pushed: 2024-08-16T08:13:10.000Z (3 months ago)
- Last Synced: 2024-10-14T20:23:58.327Z (25 days ago)
- Topics: finite-state-machine, fsm, go
- Language: Go
- Size: 72.3 KB
- Stars: 2,827
- Watchers: 51
- Forks: 312
- Open Issues: 29
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-repositories - looplab/fsm - Finite State Machine for Go (Go)
- awesome-go - FSM for Go - Finite State Machine (Data structures)
- go-awesome - FSM - finite state machine (Open source library / Algorithm)
- awesome-golang-repositories - fsm
README
[![PkgGoDev](https://pkg.go.dev/badge/github.com/looplab/fsm)](https://pkg.go.dev/github.com/looplab/fsm)
![Bulid Status](https://github.com/looplab/fsm/actions/workflows/main.yml/badge.svg)
[![Coverage Status](https://img.shields.io/coveralls/looplab/fsm.svg)](https://coveralls.io/r/looplab/fsm)
[![Go Report Card](https://goreportcard.com/badge/looplab/fsm)](https://goreportcard.com/report/looplab/fsm)# FSM for Go
FSM is a finite state machine for Go.
It is heavily based on two FSM implementations:
- Javascript Finite State Machine, https://github.com/jakesgordon/javascript-state-machine
- Fysom for Python, https://github.com/oxplot/fysom (forked at https://github.com/mriehl/fysom)
For API docs and examples see http://godoc.org/github.com/looplab/fsm
# Basic Example
From examples/simple.go:
```go
package mainimport (
"context"
"fmt""github.com/looplab/fsm"
)func main() {
fsm := fsm.NewFSM(
"closed",
fsm.Events{
{Name: "open", Src: []string{"closed"}, Dst: "open"},
{Name: "close", Src: []string{"open"}, Dst: "closed"},
},
fsm.Callbacks{},
)fmt.Println(fsm.Current())
err := fsm.Event(context.Background(), "open")
if err != nil {
fmt.Println(err)
}fmt.Println(fsm.Current())
err = fsm.Event(context.Background(), "close")
if err != nil {
fmt.Println(err)
}fmt.Println(fsm.Current())
}
```# Usage as a struct field
From examples/struct.go:
```go
package mainimport (
"context"
"fmt""github.com/looplab/fsm"
)type Door struct {
To string
FSM *fsm.FSM
}func NewDoor(to string) *Door {
d := &Door{
To: to,
}d.FSM = fsm.NewFSM(
"closed",
fsm.Events{
{Name: "open", Src: []string{"closed"}, Dst: "open"},
{Name: "close", Src: []string{"open"}, Dst: "closed"},
},
fsm.Callbacks{
"enter_state": func(_ context.Context, e *fsm.Event) { d.enterState(e) },
},
)return d
}func (d *Door) enterState(e *fsm.Event) {
fmt.Printf("The door to %s is %s\n", d.To, e.Dst)
}func main() {
door := NewDoor("heaven")err := door.FSM.Event(context.Background(), "open")
if err != nil {
fmt.Println(err)
}err = door.FSM.Event(context.Background(), "close")
if err != nil {
fmt.Println(err)
}
}
```# License
FSM is licensed under Apache License 2.0
http://www.apache.org/licenses/LICENSE-2.0