Isn't it better just to panic when you send/recv on a nil channel instead of forever blocking?

121 views
Skip to first unread message

Ronald

unread,
May 4, 2017, 11:35:09 PM5/4/17
to golang-nuts
The program will simply panic when you write to a closed channel, because that kind of operations is just nonsense after the action of closing the channel.

I afraid that the situation is similar to the nil channel operation.Because the nil channel is not been initialized yet, so there is must be some logic problem with the program if you w/r on a nil channel.

So the question:

    Isn't it better just to throw a panic when user send/recv on a nil channel instead of forever blocking it? If not, what is the benefits?

Jesse McNelis

unread,
May 5, 2017, 12:54:18 AM5/5/17
to Ronald, golang-nuts
On Fri, May 5, 2017 at 1:35 PM, Ronald <yunth...@gmail.com> wrote:
>
> So the question:
>
> Isn't it better just to throw a panic when user send/recv on a nil
> channel instead of forever blocking it? If not, what is the benefits?

There is value in the ability to have a select case be a nil channel.
In a select a nil channel is never ready to receive and thus isn't a
case that will proceed.
This is useful for allowing you to conditionally prevent certain cases
from proceeding by swapping out the channel for a nil.

While outside a select this behaviour isn't really useful, but for
consistency it works exactly the same.

Ronald

unread,
May 5, 2017, 1:26:33 AM5/5/17
to golang-nuts, yunth...@gmail.com
Wouldn't it better to just keep the nil channels out of the cases in the select
statement at the very beginning like epoll_wait does?
(I think the select statement in golang is very similar to the epoll_wait at sys/epoll.h.)

Or this is a compromise to let the programmer do anything they want?

Ronald

unread,
May 5, 2017, 1:29:17 AM5/5/17
to golang-nuts, yunth...@gmail.com


On Friday, 5 May 2017 13:26:33 UTC+8, Ronald wrote:
Wouldn't it better to just keep the nil channels out of the cases in the select
statement at the very beginning like epoll_wait does?
(I think the select statement in golang is very similar to the epoll_wait at sys/epoll.h.)

Or this is a compromise to let the programmer do anything they want?

And protect them :-)

Mikhail Mazurskiy

unread,
May 5, 2017, 7:35:58 AM5/5/17
to Ronald, golang-nuts
Hi,

I find it very useful to be able to dynamically turn certain cases in a select statement on and off. Here are two illustrations to Jesse's comment:


Cheers,
Mikhail.


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages