You can think of it this way:
With a keyword, the gofunction fibonacciadds numbers to the channel, and the loop for i := range cprints each number as soon as it is added to the channel.
Without a keyword go, the function is called fibonacci, adds all the numbers to the channel and then returns, and then the cycle forprints the numbers with channel.
- ( ):
package main
import (
"fmt"
"time"
)
func fibonacci(n int, c chan int) {
x, y := 0, 1
for i := 0; i < n; i++ {
time.Sleep(time.Second)
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)
}
}