ok is false if there are no more values to receive and the channel is closed.

    Note: Only the sender should close a channel, never the receiver. Sending on a closed channel will cause a panic.

    range-and-close.go

    1. package main
    2. import (
    3. "fmt"
    4. x, y := 0, 1
    5. for i := 0; i < n; i++ {
    6. c <- x
    7. x, y = y, x+y
    8. }
    9. close(c)
    10. func main() {
    11. c := make(chan int, 10)
    12. go fibonacci(cap(c), c)
    13. for i := range c {
    14. fmt.Println(i)
    15. }