Sobes.tech
Senior

What happens to writing goroutines when the buffer is full?

sobes.tech AI

Answer from AI

Goroutines in Go will be blocked when the channel buffer is full.

If the channel is buffered, writing to it blocks when the number of elements in the buffer reaches its capacity. Until someone reads from the channel and frees up space, the write will wait.

Example:

ch := make(chan int, 2) // buffer of 2 elements

ch <- 1 // wrote the first element
ch <- 2 // wrote the second element
ch <- 3 // here, the goroutine will block until someone reads from ch

Thus, blocking occurs when attempting to write to a full buffer.

What happens to writing goroutines when the buffer is… - sobes.tech