Suppose I have a buffered send and unbuffered receive channel:
s := make(chan<- int, 5)
r := make(<-chan int)
Is it possible to select
on them both, so that r
will be selected if it has anything to read, and s
will be selected if it is not full? Something equivalent to this, but not using 100% CPU:
for {
if len(s) < cap(s) {
// Send something
}
if len(r) > 0 {
// Receive something
}
}
Note that I want to decide what to send at the time that I send it, not earlier.
Edit
This question is basically equivalent to "Can I block until a channel is ready-to-send, without sending anything?"