RepositoryGoSESweden
LO

looplab/fsm

Finite State Machine for Go

3.4kstars
330forks
25issues
4views

Featured achievements

The project's latest awards

1
TINIX TRENDING·Topic: fsm

#1 Repository of the month

1
TINIX TRENDING·Topic: finite-state-machine

#1 Repository of the month

3
TINIX TRENDING·Country: SE

#3 Repository of the month

PkgGoDev Bulid Status Coverage Status Go 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 see http://godoc.org/github.com/looplab/fsm

Basic Example

From examples/simple.go:

package main

import (
    "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:

package main

import (
    "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

Similar Projects

Discover other similar projects and AI models.

RepositoryGo
Gurpartap/statemachine-go

🚦 Declarative Finite-State Machines in Go

111
Aug 9, 2023
RepositoryGo
avelino/awesome-go

A curated list of awesome Go frameworks, libraries and software

197.2k
Dec 2, 2026
RepositoryGo
ollama/ollama

Get up and running with Inkling, Kimi, GLM, MiniMax, DeepSeek, gpt-oss, Qwen, Gemma and other models.

188.9k
Dec 2, 2026
RepositoryPython
viewflow/django-fsm

Django friendly finite state machine support

2.3k
Nov 13, 2026
RepositoryC#
Inspiaaa/UnityHFSM

A fast, tried-and-tested hierarchical finite state machine library for Unity, designed to be easy to use yet powerful without compromising performance.

1.6k
Mar 25, 2026
RepositoryPython
fgmacedo/python-statemachine

Expressive statecharts and FSMs for modern Python.

1.3k
Oct 23, 2026
RepositoryC
katef/libfsm

DFA regular expression library & friends

987
Nov 26, 2026
RepositoryRust
mdeloof/statig

Hierarchical state machines for designing event-driven systems

814
Nov 29, 2025
RepositoryTypeScript
StoneCypher/jssm

Fast, easy Javascript finite state machines with visualizations; enjoy a one liner FSM instead of pages. MIT; Typescripted; 100% test coverage. Implements the FSL language.

373
Nov 14, 2026