Closing channels in Go
<p>I am learning how channels work in Go and have stumbled upon a problem with closing the channels. This is a modified example from <em>A Tour of Go</em>, which generates n-1 fibonacci numbers and sends them through the channels, leaving the last "element" of the channel capacity unused.</p>

<pre><code>func fibonacci(n int, c chan int) {
    x, y := 0, 1
    for i := 0; i &lt; n-1; i++ {
        c &lt;- x
        x, y = y, x+y

    }
    // close(c) // It's commented out on purpose
}

func main() {
    n := 10
    c := make(chan int, n)
    go fibonacci(n, c)

    for i := 0; i &lt; n; i++ {
        _, ok := &lt;-c
        fmt.Println(ok)
    }
}
</code></pre>

<p>The problem is that I get:</p>

<blockquote>
  <p>fatal error: all goroutines are asleep - deadlock!</p>
</blockquote>

<p>when I do not close the channel. What exactly is causing the deadlock? Why  can't I receive from the channel in its capacity boundaries when I don't close it?</p>