Go Buffered Channels
By default, channels are unbuffered, meaning they only accept sends if there is a corresponding receive ready. Buffered channels accept a limited number of values without a corresponding receiver for those values.
1. Basic Example
You specify the buffer capacity as the second argument to make.
2. Blocking Behavior
- Sending: A send into a buffered channel blocks only when the buffer is full.
- Receiving: A receive from a buffered channel blocks only when the buffer is empty.
Why use Buffered Channels?
- Performance: They can reduce overhead by allowing a "burst" of work to be sent without waiting for the consumer to catch up immediately.
- Decoupling: Senders and receivers don't have to stay in perfect "lock-step."
- Rate Limiting: They are often used to limit the number of concurrent operations (e.g., only allow 100 HTTP requests at a time).
Common Pattern: Waiting for Workers
Buffered channels are useful when you know exactly how many results you are waiting for.