FAQ
Hi all.

Recently, when reading http://blog.golang.org/pipelines I came across
this snippet of code:

select {
   case out <- n * n:
   case <-done:
     return
}

The idea is that when the channel done is closed, you return from the
function. However I had assumed that selecting on a closed channel
would be the same as though that case were missing entirely, or in
other words, that the function would never return.

After further research, it appears that what Go actually does makes
sense, though to me at least, it wasn't obvious. In the spec, in the
Receive operator section, we read:

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

In the section on the select statements, it is made clear what happens
if a channel is nil, but it does not mention what happens when
receiving from a channel that is closed. I believe it would be
helpful to add this information.

Thoughts?

Aaron

--
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.

Search Discussions

  • Brendan Tracey at May 18, 2014 at 5:31 pm
    It seems fine to me. The section you quote clearly defines the behavior,
    and the section sentence in select statements links to the "receive
    operator" section.
    On Sunday, May 18, 2014 7:08:59 AM UTC-7, Aaron Cannon wrote:

    Hi all.

    Recently, when reading http://blog.golang.org/pipelines I came across
    this snippet of code:

    select {
    case out <- n * n:
    case <-done:
    return
    }

    The idea is that when the channel done is closed, you return from the
    function. However I had assumed that selecting on a closed channel
    would be the same as though that case were missing entirely, or in
    other words, that the function would never return.

    After further research, it appears that what Go actually does makes
    sense, though to me at least, it wasn't obvious. In the spec, in the
    Receive operator section, we read:

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

    In the section on the select statements, it is made clear what happens
    if a channel is nil, but it does not mention what happens when
    receiving from a channel that is closed. I believe it would be
    helpful to add this information.

    Thoughts?

    Aaron
    --
    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.
  • Aaron Cannon at May 18, 2014 at 6:13 pm
    Thanks for the reply. I would argue that it doesn't make sense then to discuss the special case of nil channels, which is also covered elsewhere, without also discussing the special case of closed channels.

    Aaron

    --
    This message was sent from a mobile device

    On May 18, 2014, at 12:31 PM, Brendan Tracey wrote:

    It seems fine to me. The section you quote clearly defines the behavior, and the section sentence in select statements links to the "receive operator" section.
    On Sunday, May 18, 2014 7:08:59 AM UTC-7, Aaron Cannon wrote:
    Hi all.

    Recently, when reading http://blog.golang.org/pipelines I came across
    this snippet of code:

    select {
    case out <- n * n:
    case <-done:
    return
    }

    The idea is that when the channel done is closed, you return from the
    function. However I had assumed that selecting on a closed channel
    would be the same as though that case were missing entirely, or in
    other words, that the function would never return.

    After further research, it appears that what Go actually does makes
    sense, though to me at least, it wasn't obvious. In the spec, in the
    Receive operator section, we read:

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

    In the section on the select statements, it is made clear what happens
    if a channel is nil, but it does not mention what happens when
    receiving from a channel that is closed. I believe it would be
    helpful to add this information.

    Thoughts?

    Aaron
    --
    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.
    --
    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.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMay 18, '14 at 2:09p
activeMay 18, '14 at 6:13p
posts3
users2
websitegolang.org

2 users in discussion

Aaron Cannon: 2 posts Brendan Tracey: 1 post

People

Translate

site design / logo © 2022 Grokbase