example of using a select statement with channels
This commit is contained in:
parent
a8b0538b1b
commit
e3bfba2430
|
@ -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)
|
||||||
|
}
|
Loading…
Reference in New Issue