

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Still calibrating
click for more info
Not enough gems
Cost: 6 gems
1: Testing With Testify
incomplete
2: Parsing the Request Line
incomplete
3: Parsing a Stream
incomplete
4: Request Line Review
incomplete
5: State Machine
incomplete
6: Connect the Parsing
incomplete
This lesson's interactive features are locked, please to keep using them
A state machine is just a fancy word for a system that can be in one of many different states. For example, this function is not a state machine:
func add(a, b int) int {
return a + b
}
Because it doesn't have any internal state. It just takes two numbers and returns their sum. But this function is a state machine:
type Counter struct {
count int
}
func (c *Counter) Add(a int) int {
c.count += a
return c.count
}
Because it has internal state (the count field), and each time we call Add, it changes the state of the Counter struct. It's not a pure function, it's a stateful function.
The combination of RequestFromReader and Request.parse functions create our state machine. We keep track of several pieces of state:
io.Reader into the buffer