Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Stateful Solutions: A Hands-On Guide to FSM in Golang

Stateful Solutions: A Hands-On Guide to FSM in Golang

Discover the power of Finite State Machines (FSMs) in Golang. This talk offers a concise guide on implementing FSMs, enhancing code efficiency and simplifying complex logic in Go applications, and a bonus!

Ilya Kaznacheev

January 30, 2024
Tweet

More Decks by Ilya Kaznacheev

Other Decks in Programming

Transcript

  1. Finite State Machine A simple computer program that has a

    limited number of speci fi c conditions or “states”; it can be in at any given time. It starts in an initial state and then moves between states in response to certain inputs or events, following prede fi ned rules.
  2. How to implement FSM in Go • array (table) •

    map • switch-case • DSL • library • platform
  3. Events • turn on • turn off • start brewing

    • stop brewing • warmed up
  4. map

  5. DSL

  6. Practical Tips • process events atomically • save state in

    persistent storage • trace and audit events and transitions • event sourcing works well with state machines
  7. MermaidJS stateDiagram - v2 [*] - - > turned_off turned_off

    - - > warming_up: turn on warming_up - - > idle: warmed up idle - - > making_coffee: start brewing idle - - > turned_off: turn off making_coffee - - > idle: done brewing
  8. Summing Up FSM can be: • static or dynamic •

    compile-time or run-time • code- fi rst or design- fi rst • fl exible or rigid • interpreted or orchestrated
  9. I am a consultant Hire me! ‣ Microservices ‣ Kubernetes

    ‣ GCP / AWS / Azure ‣ High Load Applications ‣ Cost Optimization ‣ Compliance