I have some questions... on Buffered Channel
Q1.
Then why the NonBuffered never outputs the TRUE value?
Does this mean the unbuffered channel is always closed? Then how come the unbuffered channel still produce the right outcome of fibonacci calculation?
Q2.
Why the NonBuffered output gets cut in the middle like the following?
0 1 1 2 NB value: 0
NB ok: false
3
It should be something related with goroutine but Why in this way?
It is hard to understand because if I buffer this channel I get true value but with unbuffered channel I get only false...
Please check out the following link.
http://play.golang.org/p/O2Th61DfEY
package main
import "fmt"
func main() {
ch := make(chan int)
go NB(5, ch)
for i := range ch {
fmt.Printf("%d ", i)
}
/*
0 1 1 2 NON_Buffered value: 0
NON_Buffered ok: false
3
*/
fmt.Println()
value, ok := <-ch
fmt.Println("MAIN = NB value:", value)
fmt.Println("MAIN = NB ok:", ok)
}
func NB(n int, ch chan int) {
x, y := 0, 1
for i := 0; i < n; i++ {
ch <- x
x, y = y, x+y
}
close(ch)
value, ok := <-ch
fmt.Println("NB value:", value)
fmt.Println("NB ok:", ok)
}
Thanks a lot!