Unbuffered channel with two selects, one sending and one receiving

54 views
Skip to first unread message

Inchs Nicodi

unread,
Mar 12, 2026, 5:50:19 PM (yesterday) Mar 12
to golang-nuts
Two goroutines are using the same unbuffered channel.

One sends on it inside a select with a default clause.
The other receives from it inside a select with a default clause.

goroutine 1:
for {
    select {
    case obj <- ch:
    default:
    }
    ....
}

goroutine 2:
for {
    select {
    case ch <- obj:
    default:
    }
    ....
}

The channel is unbuffered.

Can these two goroutines ever communicate with each other?
It's a difficult thing to test if they never meet except once in a billion attempts.

I've looked at runtime/chan.go but I'm not sure if this depends on what the compiler generates for those select statements.

tapi...@gmail.com

unread,
Mar 12, 2026, 8:03:08 PM (yesterday) Mar 12
to golang-nuts
By the current official implementation, communicate will never happen.
Because the send and receive goroutines are never put into the wait queues.
Reply all
Reply to author
Forward
0 new messages