Junior — Middle
Kuidas saab kanalil hallata pufferi suurust?
sobes.tech AI
Vastus AI-lt
In Go, the buffer size of a channel 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 buffer size 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 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 reduces to specifying the second parameter when creating the channel.