In a Go `select` statement, which of these cases will prevent the statement from blocking?

Go & Rust interview question for Advanced practice.

Answer

A case that receives from a closed channel.

Explanation

The correct answer is C. A receive on a closed channel is always ready to proceed immediately; it returns the zero value for the channel's type. Because it is always ready, its case will be selected, preventing the select from blocking (or being part of the pseudo-random choice if other cases are also ready). The other options all describe blocking scenarios: receiving from a full buffered channel is not possible (you can only receive from a non-empty one), and sending to a nil or un-received-from unbuffered channel will block forever.

Related Questions