Sobes.tech
Intern

What happens to a send operation on an unbuffered channel ch := make(chan int) when one goroutine executes ch <- 1, but there is no active receiver in the program? The channel automatically expands its buffer and the receive occurs asynchronously The goroutine will block until a receiver appears The value will be discarded without an error The send will cause a panic

sobes.tech AI

Answer from AI

In Go, an unbuffered channel blocks the send operation until another goroutine receives a value from the channel. In your case, if there is no active receiver in the program, the operation ch <- 1 will block, and the goroutine performing the send will wait.

Channels do not automatically expand buffers, values are not discarded, and no panic is triggered.

Example:

ch := make(chan int) // unbuffered channel

// Sending in a separate goroutine
go func() {
    ch <- 1 // will block until someone reads
}()

// If there is no read from ch here, the send will wait