closing nil channel

2,369 views
Skip to first unread message

Martin Hurton

unread,
Oct 9, 2011, 3:06:49 PM10/9/11
to golang-nuts
What's the behaviour of close(c) when c is nil?

- Martin

Andrew Gerrand

unread,
Oct 9, 2011, 7:17:22 PM10/9/11
to golan...@googlegroups.com
A panic caused by a nil pointer dereference.

Martin Hurton

unread,
Oct 9, 2011, 7:46:54 PM10/9/11
to golan...@googlegroups.com
Did you find out by reading the language specification or by running a
simple program?

Francisco Souza

unread,
Oct 9, 2011, 7:53:00 PM10/9/11
to Martin Hurton, golan...@googlegroups.com
On Sun, Oct 9, 2011 at 8:46 PM, Martin Hurton <hur...@gmail.com> wrote:
Did you find out by reading the language specification or by running a
simple program?

"Developing the language" would be a nice answer. 

--
Francisco Souza

Andrew Gerrand

unread,
Oct 9, 2011, 7:56:56 PM10/9/11
to golan...@googlegroups.com
On Monday, October 10, 2011 10:46:54 AM UTC+11, Martin Hurton wrote:
Did you find out by reading the language specification or by running a
simple program?

It was what I expected intuitively, and I wrote a small program to test it:

package main

func main() {
    var c chan int
    close(c)
}

The spec doesn't specify it. It probably should.

Andrew

Steven Blenkinsop

unread,
Oct 9, 2011, 8:03:21 PM10/9/11
to golan...@googlegroups.com
I'm not sure. The relevantiest section of the spec is:

nil channel is never ready for communication.

Close doesn't really fit here, but the point is that, everywhere else, operations on a nil channel don't panic. Keep in mind that there is the implementation difference:

Trying to send/receive from a nil channel causes a run-time panic. 

Your test program is therefore not really reliable in this area, since the other operations on nil channels aren't working properly yet, either. 

Andrew Gerrand

unread,
Oct 9, 2011, 9:59:20 PM10/9/11
to golan...@googlegroups.com
On Monday, October 10, 2011 11:03:21 AM UTC+11, Steven Blenkinsop wrote:
On Sun, Oct 9, 2011 at 7:56 PM, Andrew Gerrand <a...@golang.org> wrote:
The spec doesn't specify it. It probably should.
I'm not sure. The relevantiest section of the spec is:

The spec doesn't even specify that a nil pointer dereference should cause a panic (an outstanding issue), so I'm not surprised to find this corner case missing also.

Andrew 
Reply all
Reply to author
Forward
0 new messages