了解缓冲通道的工作方式

Can someone explain to me how channels work in these scenerios:

  1. If a channel isn't buffered, if you send to a channel 2 messages when no message has been received yet, does it just block the app?

  2. If there is a buffer, it behaves like #1 once the bufferred amount is reached? So if the buffer is 2, after 2 messages it will just block until you recieve at least 1 message?

Since you have to set the bufferred amount, you can't just have a channel that stores an arbitrary amount of messages?

  1. Yes.
  2. Yes.
  3. No, you can't have an infinite-buffered channel.

This is covered in detail in the Go tour.

Basically, whenever a routine sends on a channel, that routine blocks until something is available to receive it. That may be the channel buffer, or it may be something trying to receive from the channel. Likewise, whenever a routine receives on a channel, that routine blocks until there is something for it to receive.