Package fsm allows you to add finite-state machines to your Go code.

Related tags

fsm
Overview

fsm PkgGoDev Build Status Coverage Status Go Report Card

Package fsm allows you to add finite-state machines to your Go code.

States and Events are defined as int consts:

const (
    StateFoo fsm.State = iota
    StateBar
)

const (
    EventFoo fsm.Event = iota
    EventBar
)

f := fsm.New(StateFoo)
f.Transition(
    fsm.On(EventFoo), fsm.Src(StateFoo),
    fsm.Dst(StateBar),
)

You can have custom checks or actions:

f.Transition(
    fsm.Src(StateFoo), fsm.Check(func () bool {
        // check something
    }),
    fsm.Call(func () {
        // do something
    }),
)

Transitions can be triggered the second time an event occurs:

f.Transition(
    fsm.On(EventFoo), fsm.Src(StateFoo), fsm.Times(2),
    fsm.Dst(StateBar),
)

Functions can be called when entering or leaving a state:

f.EnterState(StateFoo, func() {
    // do something	
})
f.Enter(func(state fsm.State) {
    // do something	
})
f.ExitState(StateFoo, func() {
    // do something	
})
f.Exit(func(state fsm.State) {
    // do something	
})

Performance

This package is much faster and does a lot less allocations than github.com/looplab/fsm:

BenchmarkCocoonSpaceFSM-12    	29371851	        40.32 ns/op	       0 B/op	       0 allocs/op
BenchmarkLooplabFSM-12        	 2438946	       487.8 ns/op	     320 B/op	       4 allocs/op

(benchmark data is for two executed transitions)

Benchmark information on https://github.com/cocoonspace/fsm-bench

Installation

go get github.com/cocoonspace/fsm

Contribution guidelines

Contributions are welcome, as long as:

  • unit tests & comments are included,
  • no external package is used.

License

MIT - See LICENSE

Issues
Releases(v1.0.1)
Owner
Cocoon Space
Cocoon Space
Go library for creating state machines

Stateless Create state machines and lightweight state machine-based workflows directly in Go code: phoneCall := stateless.NewStateMachine(stateOffHook

Quim Muntal 365 Oct 17, 2021
使用 golang 实现的订单状态机。

订单流程图 基于上述订单流转流程,使用有限状态机的方式用 Golang 代码进行实现。

新亮 18 Oct 20, 2021
hego aims to provide a consistent API for several metaheuristics

hego hego aims to provide a consistent API for several metaheuristics (black box optimization algorithms) while being performant. Even though most of

Carl 33 Sep 15, 2021
🌍 📋 A web dashboard to inspect Terraform States

?? ?? A web dashboard to inspect Terraform States

Camptocamp 1.4k Oct 13, 2021
Lima launches Linux virtual machines on macOS, with automatic file sharing, port forwarding, and containerd.

Lima: Linux-on-Mac ("macOS subsystem for Linux", "containerd for Mac")

Akihiro Suda 4.6k Oct 15, 2021
A tool for design-by-contract in Go

gocontracts gocontracts is a tool for design-by-contract in Go. It generates pre- and post-condition checks from the function descriptions so that the

null 76 Sep 22, 2021
godesim Simulate complex systems with a simple API.

godesim Simulate complex systems with a simple API. Wrangle non-linear differential equations while writing maintainable, simple code. Why Godesim?

Patricio Whittingslow 16 Oct 2, 2021
Evolutionary optimization library for Go (genetic algorithm, partical swarm optimization, differential evolution)

eaopt is an evolutionary optimization library Table of Contents Changelog Example Background Features Usage General advice Genetic algorithms Overview

Max Halford 751 Oct 12, 2021
Concurrency Lab 1 Go Example

Concurrency Lab 1 If you're stuck look at examples on Go by Example Using the lab sheet There are two ways to use the lab sheet, you can either: creat

null 0 Oct 11, 2021
Jennifer is a code generator for Go

Jennifer Jennifer is a code generator for Go. package main import ( "fmt" . "github.com/dave/jennifer/jen" ) func main() { f := NewFile("m

Dave Brophy 2.2k Oct 20, 2021
A boilerplate showing how to create a Pulumi component provider written in Go

xyz Pulumi Component Provider (Go) This repo is a boilerplate showing how to create a Pulumi component provider written in Go. You can search-replace

Pulumi 0 Oct 7, 2021
elPrep: a high-performance tool for analyzing sequence alignment/map files in sequencing pipelines.

Overview elPrep is a high-performance tool for analyzing .sam/.bam files (up to and including variant calling) in sequencing pipelines. The key advant

null 246 Oct 19, 2021
Terraform Provider for cascading runs across multiple workspaces.

Terraform Multispace Provider The multispace Terraform provider implements resources to help work with multi-workspace workflows in Terraform Cloud (o

Mitchell Hashimoto 23 Oct 18, 2021
James is your butler and helps you to create, build, debug, test and run your Go projects

go-james James is your butler and helps you to create, build, debug, test and run your Go projects. When you often create new apps using Go, it quickl

Pieter Claerhout 45 Sep 28, 2021
Go implementation of the XDG Base Directory Specification and XDG user directories

xdg Provides an implementation of the XDG Base Directory Specification. The specification defines a set of standard paths for storing application file

Adrian-George Bostan 136 Oct 12, 2021
Flow-based and dataflow programming library for Go (golang)

GoFlow - Dataflow and Flow-based programming library for Go (golang) Status of this branch (WIP) Warning: you are currently on v1 branch of GoFlow. v1

Vladimir Sibirov 1.3k Oct 16, 2021
ghw - Golang HardWare discovery/inspection library

ghw - Golang HardWare discovery/inspection library ghw is a small Golang library providing hardware inspection and discovery for Linux and Windows.

Jay Pipes 1.1k Oct 22, 2021
GitHub Review Stats

gh-review-stats - GitHub Review Stats A command line tool for examining review statistics for GitHub repositories. Installing Download a pre-built bin

Doug Hellmann 5 May 22, 2021
Support CI generation of SBOMs via golang tooling.

Software Package Data Exchange (SPDX) is an open standard for communicating software bill of materials (SBOM) information that supports accurate identification of software components, explicit mapping of relationships between components, and the association of security and licensing information with each component.

SPDX 71 Oct 20, 2021