- Notifications
You must be signed in to change notification settings - Fork329
Finite State Machine for Go
License
NotificationsYou must be signed in to change notification settings
looplab/fsm
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
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 athttps://github.com/mriehl/fysom)
For API docs and examples seehttp://godoc.org/github.com/looplab/fsm
From examples/simple.go:
package mainimport ("context""fmt""github.com/looplab/fsm")funcmain() {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")iferr!=nil {fmt.Println(err) }fmt.Println(fsm.Current())err=fsm.Event(context.Background(),"close")iferr!=nil {fmt.Println(err) }fmt.Println(fsm.Current())}
From examples/struct.go:
package mainimport ("context""fmt""github.com/looplab/fsm")typeDoorstruct {TostringFSM*fsm.FSM}funcNewDoor(tostring)*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) }, }, )returnd}func (d*Door)enterState(e*fsm.Event) {fmt.Printf("The door to %s is %s\n",d.To,e.Dst)}funcmain() {door:=NewDoor("heaven")err:=door.FSM.Event(context.Background(),"open")iferr!=nil {fmt.Println(err) }err=door.FSM.Event(context.Background(),"close")iferr!=nil {fmt.Println(err) }}
FSM is licensed under Apache License 2.0
About
Finite State Machine for Go
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
No packages published
Uh oh!
There was an error while loading.Please reload this page.