Golang中的事件驱动模型

I am reading RabbitMQ tutorial and seeing following code:

forever := make(chan bool)

go func() {
  for d := range msgs {
    log.Printf("Received a message: %s", d.Body)
  }
}()

log.Printf(" [*] Waiting for messages. To exit press CTRL+C")
<-forever

What I am interested in is for d := range msgs. How does this for loop handle events? eg. At the app start I have only one message in msgs queue buffer.

Questions:

  1. How would it process next event?
  2. After some playing around with this code I found that it could stuck on log.Printf line and wouldn't process events anymore. What can be a reason?
  1. How would it process next event?

Beside iteration over basic data structures, range in GoLang can also iterate over values received from a channel. Range iterates over each element as it's received from queue and ends only when the channel is closed. The next iteration will happen when the channel (msgs) will receive a value

msgs <- message
  1. After some playing around with this code I found that it could stuck on log.Printf line and wouldn't process events anymore. What can be a reason?

Considering that there is a blocking channel forever and we have a range that iterates over the msgs channel there are 2 possible actions that are expected:

Either

  1. send message through the msgs channel

    msgs <- message

or

  1. send value to forever so it unblocks the process

    forever <- false

It seems like the solution is designed to wait and asynchronously process messages through a channel.