diff --git a/go/statemachine.go b/go/statemachine.go new file mode 100644 index 0000000..c22280b --- /dev/null +++ b/go/statemachine.go @@ -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 + } + } +}