I am on the concurrency example here: https://tour.golang.org/concurrency/5
This is the code:
package main
import "fmt"
func fibonacci(c, quit chan int) {
x, y := 0, 1
for {
select {
case c <- x:
x, y = y, x + y
case <- quit:
fmt.Println("quit")
return
}
}
}
func main() {
c := make(chan int)
quit := make(chan int)
go func() {
for i := 0; i < 10; i++ {
fmt.Println(<- c)
}
quit <- 0
}()
fibonacci(c, quit)
}
There is quite a bit about this that is confusing to me and the tour itself doesn't say much about it. It says that select will wait on communication operations but I don't really get how that applies to the function that it sends to a goroutine before calling fibonacci
. fmt.Println
is being sent the channel c
, but I just don't see why or how this makes any sense...
I have so many questions about this, I am hoping that everything falls into place after understanding the first part...