patterns/go/statemachine.go

34 lines
573 B
Go

package main
// Ways To Do Things - Peter Bourgon - Release Party #GoSF
// from https://www.youtube.com/watch?v=LHe1Cb_Ud_M&t=259s
type fooReq struct{}
type barReq struct{}
type stateMachine struct {
state string
fooc chan fooReq
barc chan barReq
quitc chan struct{}
}
func (sm *stateMachine) foo() int {
c := make(chan int)
sm.fooc <- fooReq{c}
return <-c
}
func (sm *stateMachine) loop() {
for {
select {
case r := <-sm.fooc:
sm.handleFoo(r)
case r := <-sm.barc:
sm.handleBar(r)
case: <-sm.quitc:
return
}
}
}