I am learning Golang right now, and came across some interesting tutorial online. for example this one: https://golangbot.com/channels/
in this part about goroutines, there is one example case as following:
package main
import (
"fmt"
)
func producer(chnl chan int) {
for i := 0; i < 10; i++ {
fmt.Println("debugging send...", i)
chnl <- i
}
close(chnl)
}
func main() {
ch := make(chan int)
go producer(ch)
for {
v, ok := <-ch
if ok == false {
break
}
fmt.Println("Received ", v, ok)
}
}
the line fmt.Println("debugging send...", i)
is added by me for debugging purpose. and the output is:
debugging send... 0
debugging send... 1
Received 0 true
Received 1 true
debugging send... 2
debugging send... 3
Received 2 true
Received 3 true
debugging send... 4
debugging send... 5
Received 4 true
Received 5 true
debugging send... 6
debugging send... 7
Received 6 true
Received 7 true
debugging send... 8
debugging send... 9
Received 8 true
Received 9 true
The output order seems interesting for me, but can't fully understand what's going on under the hood.