Source file tour/concurrency/range-and-close.go

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

View as plain text