statemachine
This commit is contained in:
parent
700dcd90b1
commit
f251c538a5
33
go/statemachine.go
Normal file
33
go/statemachine.go
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user