Source file tour/concurrency/select.go

     1  //go:build OMIT
     2  
     3  package main
     4  
     5  import "fmt"
     6  
     7  func fibonacci(c, quit chan int) {
     8  	x, y := 0, 1
     9  	for {
    10  		select {
    11  		case c <- x:
    12  			x, y = y, x+y
    13  		case <-quit:
    14  			fmt.Println("quit")
    15  			return
    16  		}
    17  	}
    18  }
    19  
    20  func main() {
    21  	c := make(chan int)
    22  	quit := make(chan int)
    23  	go func() {
    24  		for i := 0; i < 10; i++ {
    25  			fmt.Println(<-c)
    26  		}
    27  		quit <- 0
    28  	}()
    29  	fibonacci(c, quit)
    30  }
    31  

View as plain text