FAQ
Hello,

I had case when I wanted to receive msg and and send message from inchan to
outchan in for-select. What I have did is following.

for {
select {
case msg = <-inchan:
case <-exitc:
exit = true
}

if exit {
break
}

select {
case outchan <- msg:
case <-exitc:
exit = true
}

if exit {
break
}
}

Any better way of doing it? *If language supports* then what I want can be
done as following, if inchan is ready to send and outchan ready to receive
then only it succeed.

for {
select {
case outchan <- inchan:
case <-exitc:
exit = true
}

if exit {
break
}
}

--
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/groups/opt_out.

Search Discussions

  • Dmitry Vyukov at Nov 29, 2013 at 11:35 am
    The shorter form is:

    loop:
    for {
    select {
      case msg := <-inchan:
    select {
    case outchan <- msg:
      case <-exitc:
    break loop
    }
    case <-exitc:
      break loop
    }
    }

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




    On Fri, Nov 29, 2013 at 3:30 PM, Niket Patel wrote:

    Hello,

    I had case when I wanted to receive msg and and send message from inchan
    to outchan in for-select. What I have did is following.

    for {
    select {
    case msg = <-inchan:
    case <-exitc:
    exit = true
    }

    if exit {
    break
    }

    select {
    case outchan <- msg:
    case <-exitc:
    exit = true
    }

    if exit {
    break
    }
    }

    Any better way of doing it? *If language supports* then what I want can
    be done as following, if inchan is ready to send and outchan ready to
    receive then only it succeed.

    for {
    select {
    case outchan <- inchan:
    case <-exitc:
    exit = true
    }

    if exit {
    break
    }
    }

    --
    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/groups/opt_out.
    --
    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/groups/opt_out.
  • Niket Patel at Nov 29, 2013 at 11:41 am
    I considered it, I don't think its shorter and nested forms are harder to
    read (thats why we have select).

    Another problem is you are only considering minimal code I have posted what
    if outchan takes 10mins to be ready other cases in outer select will not
    execute until 10 mins pass.
    On Friday, November 29, 2013 5:04:43 PM UTC+5:30, Dmitry Vyukov wrote:

    The shorter form is:

    loop:
    for {
    select {
    case msg := <-inchan:
    select {
    case outchan <- msg:
    case <-exitc:
    break loop
    }
    case <-exitc:
    break loop
    }
    }

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





    On Fri, Nov 29, 2013 at 3:30 PM, Niket Patel <nex...@gmail.com<javascript:>
    wrote:
    Hello,

    I had case when I wanted to receive msg and and send message from inchan
    to outchan in for-select. What I have did is following.

    for {
    select {
    case msg = <-inchan:
    case <-exitc:
    exit = true
    }

    if exit {
    break
    }

    select {
    case outchan <- msg:
    case <-exitc:
    exit = true
    }

    if exit {
    break
    }
    }

    Any better way of doing it? *If language supports* then what I want can
    be done as following, if inchan is ready to send and outchan ready to
    receive then only it succeed.

    for {
    select {
    case outchan <- inchan:
    case <-exitc:
    exit = true
    }

    if exit {
    break
    }
    }

    --
    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...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    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/groups/opt_out.
  • Dmitry Vyukov at Nov 29, 2013 at 11:55 am

    On Fri, Nov 29, 2013 at 3:41 PM, Niket Patel wrote:
    I considered it, I don't think its shorter and nested forms are harder to
    read (thats why we have select).

    Another problem is you are only considering minimal code I have posted what
    if outchan takes 10mins to be ready other cases in outer select will not
    execute until 10 mins pass.
    If there are more cases, and you don't want to duplicate them, then
    you can play with nilling out channels:
    http://play.golang.org/p/naIbMzjkp_

    On Friday, November 29, 2013 5:04:43 PM UTC+5:30, Dmitry Vyukov wrote:

    The shorter form is:

    loop:
    for {
    select {
    case msg := <-inchan:
    select {
    case outchan <- msg:
    case <-exitc:
    break loop
    }
    case <-exitc:
    break loop
    }
    }

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




    On Fri, Nov 29, 2013 at 3:30 PM, Niket Patel wrote:

    Hello,

    I had case when I wanted to receive msg and and send message from inchan
    to outchan in for-select. What I have did is following.

    for {
    select {
    case msg = <-inchan:
    case <-exitc:
    exit = true
    }

    if exit {
    break
    }

    select {
    case outchan <- msg:
    case <-exitc:
    exit = true
    }

    if exit {
    break
    }
    }

    Any better way of doing it? If language supports then what I want can be
    done as following, if inchan is ready to send and outchan ready to receive
    then only it succeed.

    for {
    select {
    case outchan <- inchan:
    case <-exitc:
    exit = true
    }

    if exit {
    break
    }
    }

    --
    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...@googlegroups.com.

    For more options, visit https://groups.google.com/groups/opt_out.
    --
    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/groups/opt_out.
    --
    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/groups/opt_out.
  • Niket Patel at Nov 29, 2013 at 12:04 pm

    If there are more cases, and you don't want to duplicate them, then
    you can play with nilling out channels:
    http://play.golang.org/p/naIbMzjkp_
    Dimitry, I think this should work and idiomatic too. (didn't tried it yet).
    still I wish if language supports this case then even if reader doesn't
    know nil tricks can understand.

    --
    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/groups/opt_out.
  • Dmitry Vyukov at Nov 29, 2013 at 12:19 pm

    On Fri, Nov 29, 2013 at 4:04 PM, Niket Patel wrote:
    If there are more cases, and you don't want to duplicate them, then
    you can play with nilling out channels:
    http://play.golang.org/p/naIbMzjkp_

    Dimitry, I think this should work and idiomatic too. (didn't tried it yet).
    still I wish if language supports this case then even if reader doesn't know
    nil tricks can understand.
    It's impossible to wait for all involved channels to be ready, because
    you do not even know values of some channels before some other channel
    operations complete (not saying about potential channel operations in
    f and g):
    case outc <- f(<-c1, <-g(<-c2)):
    Supporting and providing special syntax for only:
    case outchan <- inchan:
    seems inconsistent. There are immediate extensions that one will request like:
    case v1 <- c1, v2 <- c2:
    And I am not sure how it all plays with sync channels. Currently the
    condition for sync chan operation to proceed involves 2 goroutines on
    a single chan. With your proposal, it seems will be able to involve
    arbitrary complex graphs of chans and goroutines.

    --
    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/groups/opt_out.
  • Niket Patel at Nov 29, 2013 at 12:30 pm

    On Friday, November 29, 2013 5:49:05 PM UTC+5:30, Dmitry Vyukov wrote:
    On Fri, Nov 29, 2013 at 4:04 PM, Niket Patel wrote:
    If there are more cases, and you don't want to duplicate them, then
    you can play with nilling out channels:
    http://play.golang.org/p/naIbMzjkp_

    Dimitry, I think this should work and idiomatic too. (didn't tried it yet).
    still I wish if language supports this case then even if reader doesn't know
    nil tricks can understand.
    It's impossible to wait for all involved channels to be ready, because
    you do not even know values of some channels before some other channel
    operations complete (not saying about potential channel operations in
    f and g):
    case outc <- f(<-c1, <-g(<-c2)):
    Supporting and providing special syntax for only:
    case outchan <- inchan:
    seems inconsistent. There are immediate extensions that one will request
    like:
    case v1 <- c1, v2 <- c2:
    And I am not sure how it all plays with sync channels. Currently the
    condition for sync chan operation to proceed involves 2 goroutines on
    a single chan. With your proposal, it seems will be able to involve
    arbitrary complex graphs of chans and goroutines.

    I tried your suggestion, it works and read better except the need to extra
    variables nil does the trick.
    http://play.golang.org/p/KLqu8V_M4x

    Thanks, good explanation behind not supporting multiple channel operation.
    I think you will agree that special case is more natural, like joining
    pipes.

    --
    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/groups/opt_out.
  • Jan Mercl at Nov 29, 2013 at 11:43 am

    On Fri, Nov 29, 2013 at 12:34 PM, Dmitry Vyukov wrote:
    The shorter form is:
    I'd expect this even shorter form to work the same (not tested):

    package main

    func main() {
             inchan := make(chan int)
             outchan := make(chan int)
             exitc := make(chan int)
    loop:
             for {
                     select {
                     case outchan <- <-inchan:
                     case <-exitc:
                             break loop
                     }
             }
    }

    http://play.golang.org/p/56UTFUGUw8

    -j

    --
    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/groups/opt_out.
  • Niket Patel at Nov 29, 2013 at 11:57 am
    Jan,

    That is the first thing I tried instinctively but problem is that doesn't
    detect if exitc is ready and keep blocking if inchan is not ready

    Dimitry,

    Thanks for break loop suggestion it reads better, although nested form
    doesn't.

    --
    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/groups/opt_out.
  • Dmitry Vyukov at Nov 29, 2013 at 11:57 am

    On Fri, Nov 29, 2013 at 3:43 PM, Jan Mercl wrote:
    On Fri, Nov 29, 2013 at 12:34 PM, Dmitry Vyukov wrote:
    The shorter form is:
    I'd expect this even shorter form to work the same (not tested):

    It works not the way you think it works. Every select case waits for
    only 1 channel operation. Think about:
    case outc <- f(<-c1, <-c2, <-c3, <-c4):

    package main

    func main() {
    inchan := make(chan int)
    outchan := make(chan int)
    exitc := make(chan int)
    loop:
    for {
    select {
    case outchan <- <-inchan:
    case <-exitc:
    break loop
    }
    }
    }

    http://play.golang.org/p/56UTFUGUw8

    -j
    --
    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/groups/opt_out.
  • Jan Mercl at Nov 29, 2013 at 12:03 pm

    On Fri, Nov 29, 2013 at 12:56 PM, Dmitry Vyukov wrote:
    It works not the way you think it works. Every select case waits for
    only 1 channel operation. Think about:
    case outc <- f(<-c1, <-c2, <-c3, <-c4):
    Thanks! Got it now - not when reading the specs.

    -j

    --
    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/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedNov 29, '13 at 11:31a
activeNov 29, '13 at 12:30p
posts11
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase