Junior — Middle
How can the buffer size in a channel be managed?
sobes.tech AI
Answer from AI
In Go, the size of a channel buffer is set at creation time using the make function. If you specify the second parameter — it will be the buffer size, i.e., the number of elements the channel can hold without blocking the sender.
Example of creating a buffered channel with a buffer size of 5:
ch := make(chan int, 5)
If the buffer is full, sending to the channel blocks until someone reads from the channel. If the buffer size is 0 (or the parameter is not specified), the channel is unbuffered, and sending blocks until a value is received.
Thus, managing the buffer size boils down to specifying the second parameter when creating the channel.