Sobes.tech
Senior

What is the buffer size of a non-buffered channel?

sobes.tech AI

Answer from AI

A non-buffered channel in Go has a buffer size of zero. This means that sending to such a channel blocks until another goroutine reads the value from the channel, and vice versa — reading blocks until a value is available for reading.

Example:

ch := make(chan int) // non-buffered channel

// Sending and receiving must happen simultaneously, otherwise there will be a block
go func() {
    ch <- 42 // blocks until someone reads
}()

val := <-ch // reading unblocks the send
fmt.Println(val) // 42