From f251c538a599add2f41e360bb37c8101fd56c8b6 Mon Sep 17 00:00:00 2001 From: sneak Date: Tue, 29 Sep 2020 16:57:18 -0700 Subject: [PATCH] statemachine --- go/statemachine.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 go/statemachine.go 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 + } + } +}