FAQ
Is there a way to forward a value from one channel to another without
blocking and without wrapping one select case inside another (both with a
default case)?

I was hoping this would work, but as far as I can tell the receive is
blocking as usual and only the write can trigger the default case if
nothing is writing to the "two" channel: http://play.golang.org/p/FlpE4PUztZ

Try commenting out `one <- true` to see the code deadlock.

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

  • Blixt at Apr 1, 2015 at 5:11 am
    And to show what I meant by wrapping select cases, here's a less eloquent
    but fixed version: http://play.golang.org/p/hZ9nHhCUDd
    On Wednesday, April 1, 2015 at 1:08:46 AM UTC-4, Blixt wrote:

    Is there a way to forward a value from one channel to another without
    blocking and without wrapping one select case inside another (both with a
    default case)?

    I was hoping this would work, but as far as I can tell the receive is
    blocking as usual and only the write can trigger the default case if
    nothing is writing to the "two" channel:
    http://play.golang.org/p/FlpE4PUztZ

    Try commenting out `one <- true` to see the code deadlock.
    --
    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.
  • Dustin at Apr 1, 2015 at 5:18 am
    The deadlock occurs because according to the spec, the right-hand side of
    each of the cases of the select statement are evaluated upon entering the
    statement.
    This means that once it hits the select, it blocks waiting for one to
    receive, which it never does when you comment out the send.

    https://golang.org/ref/spec#Select_statements

    On Wednesday, April 1, 2015 at 1:08:46 AM UTC-4, Blixt wrote:

    Is there a way to forward a value from one channel to another without
    blocking and without wrapping one select case inside another (both with a
    default case)?

    I was hoping this would work, but as far as I can tell the receive is
    blocking as usual and only the write can trigger the default case if
    nothing is writing to the "two" channel:
    http://play.golang.org/p/FlpE4PUztZ

    Try commenting out `one <- true` to see the code deadlock.
    --
    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.
  • Blixt at Apr 1, 2015 at 5:25 am
    Yup that's what I thought. Is there a more eloquent way to do what I did in
    the second playground though?
    On Wednesday, April 1, 2015 at 1:18:10 AM UTC-4, Dustin wrote:

    The deadlock occurs because according to the spec, the right-hand side of
    each of the cases of the select statement are evaluated upon entering the
    statement.
    This means that once it hits the select, it blocks waiting for one to
    receive, which it never does when you comment out the send.

    https://golang.org/ref/spec#Select_statements

    On Wednesday, April 1, 2015 at 1:08:46 AM UTC-4, Blixt wrote:

    Is there a way to forward a value from one channel to another without
    blocking and without wrapping one select case inside another (both with a
    default case)?

    I was hoping this would work, but as far as I can tell the receive is
    blocking as usual and only the write can trigger the default case if
    nothing is writing to the "two" channel:
    http://play.golang.org/p/FlpE4PUztZ

    Try commenting out `one <- true` to see the code deadlock.
    --
    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.
  • Dustin at Apr 1, 2015 at 5:29 am
    I've run into the same problem in the past and haven't found a nicer
    solution than the double-select as you did.
    It's possible someone else has found a better solution. Sorry I couldn't
    give you a more helpful answer.
    On Wednesday, April 1, 2015 at 1:25:48 AM UTC-4, Blixt wrote:

    Yup that's what I thought. Is there a more eloquent way to do what I did
    in the second playground though?
    On Wednesday, April 1, 2015 at 1:18:10 AM UTC-4, Dustin wrote:

    The deadlock occurs because according to the spec, the right-hand side of
    each of the cases of the select statement are evaluated upon entering the
    statement.
    This means that once it hits the select, it blocks waiting for one to
    receive, which it never does when you comment out the send.

    https://golang.org/ref/spec#Select_statements

    On Wednesday, April 1, 2015 at 1:08:46 AM UTC-4, Blixt wrote:

    Is there a way to forward a value from one channel to another without
    blocking and without wrapping one select case inside another (both with a
    default case)?

    I was hoping this would work, but as far as I can tell the receive is
    blocking as usual and only the write can trigger the default case if
    nothing is writing to the "two" channel:
    http://play.golang.org/p/FlpE4PUztZ

    Try commenting out `one <- true` to see the code deadlock.
    --
    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.
  • Benjamin Measures at Apr 1, 2015 at 4:34 pm

    On Wednesday, 1 April 2015 06:08:46 UTC+1, Blixt wrote:
    Is there a way to forward a value from one channel to another without
    blocking and without wrapping one select case inside another (both with a
    default case)?
    It's a difficult one. Synchronous channels also pass control between
    goroutines (establishing "happens before"). There can be no single (atomic)
    transfer of control between two goroutines where mediated by a select in a
    third goroutine.

    Thankfully, channels are first class, so the usual solution is to pass a
    channel of channel. That way, the select in the third goroutine passes one
    of the goroutines a channel for it to interact with the other directly.

    --
    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
postedApr 1, '15 at 5:08a
activeApr 1, '15 at 4:34p
posts6
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase