From e3bfba2430f96f0c39e6e9afa42e571730a8c876 Mon Sep 17 00:00:00 2001 From: "Stephen M. McQuay" Date: Mon, 3 Sep 2012 08:38:21 -0600 Subject: [PATCH] example of using a select statement with channels --- concurrency/select-ex/go.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 concurrency/select-ex/go.go diff --git a/concurrency/select-ex/go.go b/concurrency/select-ex/go.go new file mode 100644 index 0000000..26b5534 --- /dev/null +++ b/concurrency/select-ex/go.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 < 20; i++ { + fmt.Println(<-c) + } + quit <- 0 + }() + fibonacci(c, quit) +}