FAQ
Hello,

I think I am missing how close and select work together.

http://play.golang.org/p/EwmpUrVL_7

I created two int channels a and b with buffer set to 3 and sent six
numbers in total there, channel a is closed then, b is left unclosed. Then
in a loop of ten cycles I read from channels.

No matter if I close a channel or not, channels keeps giving me "empty
value" (zero in case of integer). I would expect this behavior:

- if both a and b are not closed, select blocks
- if a is closed then it should print empty values from a
- the same for b
- if both are closed, default block is executed

Why select does not block?

--

Search Discussions

  • Minux at Jan 1, 2013 at 7:45 pm

    On Wed, Jan 2, 2013 at 3:40 AM, Lukáš Zapletal wrote:

    Hello,

    I think I am missing how close and select work together.

    http://play.golang.org/p/EwmpUrVL_7

    I created two int channels a and b with buffer set to 3 and sent six
    numbers in total there, channel a is closed then, b is left unclosed. Then
    in a loop of ten cycles I read from channels.

    No matter if I close a channel or not, channels keeps giving me "empty
    value" (zero in case of integer). I would expect this behavior:

    - if both a and b are not closed, select blocks
    - if a is closed then it should print empty values from a
    - the same for b
    - if both are closed, default block is executed

    Why select does not block?
    because a closed channel is always ready for read (return the empty value),
    you can do this
    http://play.golang.org/p/Phx56qVuQy
    if you don't want to always receiving empty values from a closed channel.

    --
  • Steven Blenkinsop at Jan 1, 2013 at 9:01 pm
    And if you want to have a "default" that only gets executed after all
    channels have been closed, you can do it like this:
    http://play.golang.org/p/EJPJQJ9gVJ

    On Tue, Jan 1, 2013 at 2:44 PM, minux wrote:


    On Wed, Jan 2, 2013 at 3:40 AM, Lukáš Zapletal wrote:

    Hello,

    I think I am missing how close and select work together.

    http://play.golang.org/p/EwmpUrVL_7

    I created two int channels a and b with buffer set to 3 and sent six
    numbers in total there, channel a is closed then, b is left unclosed. Then
    in a loop of ten cycles I read from channels.

    No matter if I close a channel or not, channels keeps giving me "empty
    value" (zero in case of integer). I would expect this behavior:

    - if both a and b are not closed, select blocks
    - if a is closed then it should print empty values from a
    - the same for b
    - if both are closed, default block is executed

    Why select does not block?
    because a closed channel is always ready for read (return the empty
    value), you can do this
    http://play.golang.org/p/Phx56qVuQy
    if you don't want to always receiving empty values from a closed channel.

    --

    --
  • Thorsten von Eicken at Jan 1, 2013 at 7:47 pm
    Doesn't seem very intuitive at first, but if select didn't return something
    for channel a then how could you be notified that it got closed?
    From http://golang.org/ref/spec#Receive_operator :

    Receiving from a nil channel blocks forever. Receiving from a closed<http://golang.org/ref/spec#Close> channel
    always succeeds, immediately returning the element type's zero value<http://golang.org/ref/spec#The_zero_value>
    .
    On Tuesday, January 1, 2013 11:40:45 AM UTC-8, Lukáš Zapletal wrote:

    Hello,

    I think I am missing how close and select work together.

    http://play.golang.org/p/EwmpUrVL_7

    I created two int channels a and b with buffer set to 3 and sent six
    numbers in total there, channel a is closed then, b is left unclosed. Then
    in a loop of ten cycles I read from channels.

    No matter if I close a channel or not, channels keeps giving me "empty
    value" (zero in case of integer). I would expect this behavior:

    - if both a and b are not closed, select blocks
    - if a is closed then it should print empty values from a
    - the same for b
    - if both are closed, default block is executed

    Why select does not block?
    --
  • Lukáš Zapletal at Jan 1, 2013 at 7:53 pm
    Hah! Thanks

    I vote for writing this here: http://tour.golang.org/#65

    LZ
    On Tuesday, January 1, 2013 8:47:39 PM UTC+1, Thorsten von Eicken wrote:

    Doesn't seem very intuitive at first, but if select didn't return
    something for channel a then how could you be notified that it got closed?
    From http://golang.org/ref/spec#Receive_operator :

    Receiving from a nil channel blocks forever. Receiving from a closed<http://golang.org/ref/spec#Close> channel
    always succeeds, immediately returning the element type's zero value<http://golang.org/ref/spec#The_zero_value>
    .
    On Tuesday, January 1, 2013 11:40:45 AM UTC-8, Lukáš Zapletal wrote:

    Hello,

    I think I am missing how close and select work together.

    http://play.golang.org/p/EwmpUrVL_7

    I created two int channels a and b with buffer set to 3 and sent six
    numbers in total there, channel a is closed then, b is left unclosed. Then
    in a loop of ten cycles I read from channels.

    No matter if I close a channel or not, channels keeps giving me "empty
    value" (zero in case of integer). I would expect this behavior:

    - if both a and b are not closed, select blocks
    - if a is closed then it should print empty values from a
    - the same for b
    - if both are closed, default block is executed

    Why select does not block?
    --
  • Minux at Jan 1, 2013 at 8:43 pm

    On Wed, Jan 2, 2013 at 3:53 AM, Lukáš Zapletal wrote:

    I vote for writing this here: http://tour.golang.org/#65
    There are already 2 notes on that single page....
    I'm afraid adding too many details there will overwhelm the learner.

    The Go language specification is always a must-read for Go users.

    --
  • Lukáš Zapletal at Jan 1, 2013 at 9:00 pm
    I am afraid close and select does not work well together. Once I close a
    channel, blocking is gone so I am not able to leverage select effectively
    and I am ending up in a loops. I guess I need to refactor my program again,
    I guess close is not good in my case.

    I understand that the tour page would be overhelmed, yeah.

    Thanks.

    LZ
    On Tuesday, January 1, 2013 9:43:27 PM UTC+1, minux wrote:


    On Wed, Jan 2, 2013 at 3:53 AM, Lukáš Zapletal <luk...@zapletalovi.com<javascript:>
    wrote:
    I vote for writing this here: http://tour.golang.org/#65
    There are already 2 notes on that single page....
    I'm afraid adding too many details there will overwhelm the learner.

    The Go language specification is always a must-read for Go users.
    --
  • Jesse McNelis at Jan 1, 2013 at 9:25 pm

    On Wed, Jan 2, 2013 at 8:00 AM, Lukáš Zapletal wrote:

    I am afraid close and select does not work well together. Once I close a
    channel, blocking is gone so I am not able to leverage select effectively
    and I am ending up in a loops. I guess I need to refactor my program again,
    I guess close is not good in my case.

    Receiving from a closed channel never blocks.
    Receiving from a nil channel blocks forever.

    These two rules can be combined in many ways in select.


    --
    =====================
    http://jessta.id.au

    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJan 1, '13 at 7:40p
activeJan 1, '13 at 9:25p
posts8
users5
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase