Sobes.tech
Junior — Middle

Where is the value of a variable stored when data is sent to a channel without buffering?

sobes.tech AI

Answer from AI

In Go, when sending a value to an unbuffered channel, the value is not stored inside the channel but is transferred directly from the sender to the receiver.

This means that the send operation blocks until another goroutine performs a receive operation from this channel. Thus, the value is effectively "handed over" between the sender and receiver, rather than being stored somewhere inside the channel.

Example:

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

// Sending
go func() {
    ch <- 42 // blocks until someone reads
}()

// Receiving
val := <-ch // unblocks sender
fmt.Println(val) // 42

Therefore, in an unbuffered channel, there is no internal storage for values — the transfer is synchronized between the sender and receiver.