Sobes.tech
Junior — Middle

When exactly does blocking occur when using a channel with a size of 100 elements?

sobes.tech AI

Answer from AI

In Go, a channel with a buffer size of 100 elements means that you can send up to 100 values without blocking the sender.

Blocking occurs when the buffer already contains 100 elements, and the goroutine trying to send the 101st element. Since the buffer is full, the send will be blocked until someone reads at least one element from the channel.

Example:

ch := make(chan int, 100)

// Filling the channel
for i := 0; i < 100; i++ {
    ch <- i // does not block
}

// Next send will block until an element is read
ch <- 101 // blocks

Thus, blocking occurs when attempting to send an element to a fully filled channel buffer.

When exactly does blocking occur when using a channel… - sobes.tech