go-concurrency101/basics/select/main.go

33 lines
366 B
Go

package main
import "log"
func fibonacci(ch, quit chan int) {
x, y := 0, 1
for {
select {
case ch <- x:
x, y = y, x+y
case <-quit:
log.Println("quit")
return
}
}
}
func main() {
ch := make(chan int, 10)
quit := make(chan int)
go func() {
for i := 0; i < cap(ch); i++ {
log.Println(<-ch)
}
quit <- 0
}()
fibonacci(ch, quit)
}