Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spec: closed channel behaviour clarifications #7785

Closed
gopherbot opened this issue Apr 15, 2014 · 5 comments
Closed

spec: closed channel behaviour clarifications #7785

gopherbot opened this issue Apr 15, 2014 · 5 comments
Milestone

Comments

@gopherbot
Copy link

by glyn.normington:

There are some places in the Go specification where the behaviour of closed channels
could be described more clearly.

The Channel types section says:

    A channel may be closed with the built-in function close; the multi-valued assignment form of the
    receive operator tests whether a channel has been closed.

The last statement isn't true until the channel becomes empty. If you use a multi-valued
assignment to receive from a non-empty, closed channel, you won't be able to tell the
channel has been closed (as you'll successfully receive the next value in the channel).

The Receive operator section says: 

    A receive operation on a closed channel can always proceed immediately, yielding the element
    type's zero value.

Again, this is only true of a closed, empty channel. A closed, non-empty channel will
yield the next value in the channel.

A little later, the "comma ok" form of receive is described:

    The value of ok is true if the value received was delivered by a successful send operation to
    the channel, or false if it is a zero value generated because the channel is closed and empty. 

This could perhaps mention that ok will be true in the case of a non-empty, closed
channel.

That said, the section on Close is unambiguous:

    For a channel c, the built-in function close(c) records that no more values will be sent on the
    channel. It is an error if c is a receive-only channel. Sending to or closing a closed channel
    causes a run-time panic. Closing the nil channel also causes a run-time panic. After calling
    close, and after any previously sent values have been received, receive operations will return
    the zero value for the channel's type without blocking. The multi-valued receive operation
    returns a received value along with an indication of whether the channel is closed.

So when the specification is read as whole, there isn't a problem. But perhaps the
wording of the "Channel types" and "Receive operator" sections could
be improved so as not to give the wrong impression.

(This issue was aired first here:
https://groups.google.com/forum/#!searchin/Golang-nuts/normington/golang-nuts/QjBhvNsia4I/YDDaAzNEHC4J)
@ianlancetaylor
Copy link
Contributor

Comment 1:

Labels changed: added repo-main.

Owner changed to @griesemer.

@griesemer
Copy link
Contributor

Comment 2:

Labels changed: added release-go1.3maybe.

Status changed to Accepted.

@gopherbot
Copy link
Author

Comment 3:

CL https://golang.org/cl/94030045 mentions this issue.

@griesemer
Copy link
Contributor

Comment 4:

Status changed to Started.

@griesemer
Copy link
Contributor

Comment 5:

This issue was closed by revision 97aa90d.

Status changed to Fixed.

@rsc rsc added this to the Go1.3 milestone Apr 14, 2015
@golang golang locked and limited conversation to collaborators Jun 25, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants