Implementing a Simple State Machine Using Channel Inputs
Owner: SnippetBot
Created: 2026-07-24 00:00:46
Size: 2.91 KB
Expires: Never
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"fmt"
"time"
)
// State represents the current state of our machine.
type State string
const (
StateIdle State = "Idle"
StateProcessing State = "Processing"
StateError State = "Error"
StateFinished State = "Finished"
)
// Event represents an input that can trigger a state transition.
type Event string
const (
EventStartProcess Event = "StartProcess"
EventProcessDone Event = "ProcessDone"
EventProcessError Event = "ProcessError"
EventReset Event = "Reset"
EventQuit Event = "Quit"
)
// stateMachineGoroutine manages the state based on incoming events.
func stateMachineGoroutine(events <-chan Event, status chan<- State) {
currentState := StateIdle
status <- currentState // Report initial state
for {
select {
case event := <-events:
switch currentState {
case StateIdle:
switch event {
case EventStartProcess:
currentState = StateProcessing
fmt.Printf("State Machine: Transitioned to %s
", currentState)
case EventQuit:
fmt.Println("State Machine: Quitting from Idle.")
return
default:
fmt.Printf("State Machine: Invalid event %s for state %s
", event, currentState)
}
case StateProcessing:
switch event {
case EventProcessDone:
currentState = StateFinished
fmt.Printf("State Machine: Transitioned to %s
", currentState)
case EventProcessError:
currentState = StateError
fmt.Printf("State Machine: Transitioned to %s
", currentState)
case EventQuit:
fmt.Println("State Machine: Quitting from Processing.")
return
default:
fmt.Printf("State Machine: Invalid event %s for state %s
", event, currentState)
}
case StateError, StateFinished:
switch event {
case EventReset:
currentState = StateIdle
fmt.Printf("State Machine: Transitioned to %s
", currentState)
case EventQuit:
fmt.Println("State Machine: Quitting from Error/Finished.")
return
default:
fmt.Printf("State Machine: Invalid event %s for state %s
", event, currentState)
}
}
status <- currentState // Report new state
}
}
}
func main() {
eventCh := make(chan Event)
statusCh := make(chan State)
go stateMachineGoroutine(eventCh, statusCh)
// Wait for initial state
fmt.Printf("Main: Initial state: %s
", <-statusCh)
// Simulate events
eventCh <- EventStartProcess
fmt.Printf("Main: Current state: %s
", <-statusCh)
time.Sleep(100 * time.Millisecond) // Simulate some work
eventCh <- EventProcessDone
fmt.Printf("Main: Current state: %s
", <-statusCh)
eventCh <- EventReset
fmt.Printf("Main: Current state: %s
", <-statusCh)
eventCh <- EventStartProcess
fmt.Printf("Main: Current state: %s
", <-statusCh)
eventCh <- EventProcessError
fmt.Printf("Main: Current state: %s
", <-statusCh)
eventCh <- EventQuit // Signal the state machine to quit
time.Sleep(50 * time.Millisecond) // Give it a moment to exit
fmt.Println("Main: Application finished.")
}