Here is the code example in "A Tour of Go" Range and Close:
package main
import (
"fmt"
)
func fibonacci(n int, c chan int) {
x, y := 0, 1
for i := 0; i < n; i++ {
c <- x
x, y = y, x+y
}
close(c)
}
func main() {
c := make(chan int, 10)
go fibonacci(cap(c), c)
for i := range c {
fmt.Println(i)
}
}
On the fifth line from the bottom, when the go
keyword was omitted, the result did not change. Did that mean the main goroutine sent values in the buffered channel and then took them out?