Tried running the below code(producer & consumer) to understand goroutines and channel in golang (removed package and imports from the code snippet below):
var done = make(chan bool)
var msgs = make(chan int)
func produce() {
for i := 0; i < 10; i++ {
msgs <- i
}
fmt.Println("Before closing channel")
close(msgs)
fmt.Println("Before passing true to done")
done <- true
}
func consume() {
for {
msg := <-msgs
time.Sleep(100 * time.Millisecond)
fmt.Println("Consumer: ", msg)
}
}
func main() {
go produce()
go consume()
<-done
fmt.Println("After calling DONE")
}
source code from: http://www.golangpatterns.info/concurrency/producer-consumer
Below is the output when I run the code
Consumer: 0
Consumer: 1
Consumer: 2
Consumer: 3
Consumer: 4
Consumer: 5
Consumer: 6
Consumer: 7
Consumer: 8
Before closing channel
Before passing true to done
After calling DONE
Based on my understanding with goroutines and channel: When we call produce() and consume() from main() using the go keyword, go runtime kicks of 2 goroutines(sort of threads in java world but not actual OS threads) and the main() goroutine comes and halts at "<-done". Now inside produce() - the loop goes from 0 to 9 and inside the loop the msgs channel receives the int (0 to 9) 1 at a time which is being consumed in parallel by the consume(); however produce has no clue about it and it just keep looping for 0 to 9.
Question: Assuming my above understanding is correct. Once, the for loop is done why is the next printLine inside produce() not getting printed and also why is the msgs channel not getting closed? Why is the goroutine halting inside the produce() until the consumer consumes all the msgs?