statemachine

This commit is contained in:
Jeffrey Paul 2020-09-29 16:57:18 -07:00
parent 700dcd90b1
commit f251c538a5

33
go/statemachine.go Normal file
View File

@ -0,0 +1,33 @@
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
}
}
}