Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Finite State Machine for Go

License

NotificationsYou must be signed in to change notification settings

looplab/fsm

Repository files navigation

PkgGoDevBulid StatusCoverage StatusGo Report Card

FSM for Go

FSM is a finite state machine for Go.

It is heavily based on two FSM implementations:

For API docs and examples seehttp://godoc.org/github.com/looplab/fsm

Basic Example

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())}

Usage as a struct field

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)    }}

License

FSM is licensed under Apache License 2.0

http://www.apache.org/licenses/LICENSE-2.0

About

Finite State Machine for Go

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors24


[8]ページ先頭

©2009-2025 Movatter.jp