From b01f9d70fda56243a68ffed1769828e9887bb7c2 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sat, 1 Dec 2012 07:52:03 -0500 Subject: [PATCH] Doing the bit of the go tour on `select` --- .../gotour-artifacts/selects/main.go | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 gotime/src/gotime.example.org/gotour-artifacts/selects/main.go diff --git a/gotime/src/gotime.example.org/gotour-artifacts/selects/main.go b/gotime/src/gotime.example.org/gotour-artifacts/selects/main.go new file mode 100644 index 0000000..1445318 --- /dev/null +++ b/gotime/src/gotime.example.org/gotour-artifacts/selects/main.go @@ -0,0 +1,28 @@ +package main + +import "fmt" + +func fibonacci(c, quit chan int) { + x, y := 0, 1 + for { + select { + case c <- x: + x, y = y, x + y + case <- quit: + fmt.Println("quit") + return + } + } +} + +func main() { + c := make(chan int) + quit := make(chan int) + go func() { + for i := 0; i < 10; i++ { + fmt.Println(<-c) + } + quit <- 0 + }() + fibonacci(c, quit) +}