FAQ
Dear all,

Here is a question about channels: Is it possible to use a select statement
to wait until a receiver on a channel is ready, and only then to compute the
value to send?

Example: is it possible to set up a "chan time.Time" such that reading the
channel gives the current time? I tried the following:

c := make(chan time.Time)
go func() {
for {
select {
case c <- time.Now():
  // I would like for time.Now() to be evaluated only when a reader is ready.
}
}
}()

This code doesn't work; reading from c returns the time of the previous read
(see http://play.golang.org/p/o1o8jvmtdf for illustration). Is there a way
to
modify the code to get the current time?

(This is only an example; in my real problem, data is accumulated over
time and the channel should return a hash of the data accumulated so far.)

Many thanks,
Jochen
--
http://seehuhn.de/

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

  • Jimmy frasche at Jul 31, 2013 at 5:02 pm
    You could use two chans, ready and work. The client signals ready and
    then waits for work. The server waits for ready and then computes a
    value to send down work:

    Client:
    ready <- true
    unit := <- work

    Server:
    <-ready
    work <- f()

    --
    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.
  • Kyle Lemons at Jul 31, 2013 at 5:11 pm
    a slightly different approach is to send the channel on which you want the
    answer back:

    (client)
    reply := make(chan ResT)
    server <- reply
    unit := <-reply

    (server)
    ch := <-server
    ch <- time.Now()

    On Wed, Jul 31, 2013 at 10:02 AM, jimmy frasche wrote:

    You could use two chans, ready and work. The client signals ready and
    then waits for work. The server waits for ready and then computes a
    value to send down work:

    Client:
    ready <- true
    unit := <- work

    Server:
    <-ready
    work <- f()

    --
    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.
  • Jochen Voss at Jul 31, 2013 at 6:06 pm
    Hi Kyle,
    On Wednesday, 31 July 2013 18:11:31 UTC+1, Kyle Lemons wrote:

    a slightly different approach is to send the channel on which you want the
    answer back:

    (client)
    reply := make(chan ResT)
    server <- reply
    unit := <-reply

    (server)
    ch := <-server
    ch <- time.Now()
    Thanks for your suggestion, this looks like a nice idea to me :)

    All the best,
    Jochen
    --
    http://seehuhn.de/

    --
    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.
  • Jochen Voss at Jul 31, 2013 at 6:05 pm
    Hi soapboxcicero,
    On Wednesday, 31 July 2013 18:02:19 UTC+1, soapboxcicero wrote:

    You could use two chans, ready and work. The client signals ready and
    then waits for work. The server waits for ready and then computes a
    value to send down work:

    Client:
    ready <- true
    unit := <- work

    Server:
    <-ready
    work <- f()
    Oh, I had't thought about this, thanks for the suggestion.
    Is this safe for concurrent client requests?

    All the best,
    Jochen
    --
    http://seehuhn.de/

    --
    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.
  • Jimmy frasche at Jul 31, 2013 at 6:11 pm
    If you have multiple clients it gets racy unless they have their own
    pair of channels. In that case Kyle's approach is better because you
    get both the signal and the destination in one fell swoop. You know
    that the channel you received is ready for work and that when you
    write to it goes to the correct client.

    --
    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.
  • Josh Bleecher Snyder at Jul 31, 2013 at 7:31 pm
    In addition to the other replies, see this thread:
    https://groups.google.com/forum/#!searchin/golang-nuts/lazy/golang-nuts/qZO4kldyWak/LbUUKXZkSsYJ

    (Sorry for the search url; couldn't figure out how to clean it. I wish
    Google Groups added a permalink to each outbound email. :/)

    -josh


    On Wed, Jul 31, 2013 at 9:53 AM, Jochen Voss wrote:

    Dear all,

    Here is a question about channels: Is it possible to use a select statement
    to wait until a receiver on a channel is ready, and only then to compute
    the
    value to send?

    Example: is it possible to set up a "chan time.Time" such that reading the
    channel gives the current time? I tried the following:

    c := make(chan time.Time)
    go func() {
    for {
    select {
    case c <- time.Now():
    // I would like for time.Now() to be evaluated only when a reader is
    ready.
    }
    }
    }()

    This code doesn't work; reading from c returns the time of the previous
    read
    (see http://play.golang.org/p/o1o8jvmtdf for illustration). Is there a
    way to
    modify the code to get the current time?

    (This is only an example; in my real problem, data is accumulated over
    time and the channel should return a hash of the data accumulated so far.)

    Many thanks,
    Jochen
    --
    http://seehuhn.de/

    --
    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.
  • Jsor at Jul 31, 2013 at 7:38 pm
    What about sending a function, and evaluating it upon receive? This isn't
    perfectly equivalent, and may not work in some cases, but if your only
    concern is evaluating right when the reader is ready to deal with it it
    should work. See:

    http://play.golang.org/p/gMXPFpZDXW
    On Wednesday, July 31, 2013 12:31:01 PM UTC-7, Joshua Bleecher Snyder wrote:

    In addition to the other replies, see this thread:
    https://groups.google.com/forum/#!searchin/golang-nuts/lazy/golang-nuts/qZO4kldyWak/LbUUKXZkSsYJ

    (Sorry for the search url; couldn't figure out how to clean it. I wish
    Google Groups added a permalink to each outbound email. :/)

    -josh



    On Wed, Jul 31, 2013 at 9:53 AM, Jochen Voss <joche...@gmail.com<javascript:>
    wrote:
    Dear all,

    Here is a question about channels: Is it possible to use a select
    statement
    to wait until a receiver on a channel is ready, and only then to compute
    the
    value to send?

    Example: is it possible to set up a "chan time.Time" such that reading the
    channel gives the current time? I tried the following:

    c := make(chan time.Time)
    go func() {
    for {
    select {
    case c <- time.Now():
    // I would like for time.Now() to be evaluated only when a reader is
    ready.
    }
    }
    }()

    This code doesn't work; reading from c returns the time of the previous
    read
    (see http://play.golang.org/p/o1o8jvmtdf for illustration). Is there a
    way to
    modify the code to get the current time?

    (This is only an example; in my real problem, data is accumulated over
    time and the channel should return a hash of the data accumulated so far.)

    Many thanks,
    Jochen
    --
    http://seehuhn.de/

    --
    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.
  • Carlos Castillo at Aug 1, 2013 at 2:27 am
    Well, technically, in that scenario, you could just evaluate the function
    once you got a signal from the channel:

    timeout := time.After(5 * time.Milliseconds)
    <-timeout
    x := time.Now()

    On Wednesday, July 31, 2013 12:38:28 PM UTC-7, Jsor wrote:

    What about sending a function, and evaluating it upon receive? This isn't
    perfectly equivalent, and may not work in some cases, but if your only
    concern is evaluating right when the reader is ready to deal with it it
    should work. See:

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

    On Wednesday, July 31, 2013 12:31:01 PM UTC-7, Joshua Bleecher Snyder
    wrote:
    In addition to the other replies, see this thread:
    https://groups.google.com/forum/#!searchin/golang-nuts/lazy/golang-nuts/qZO4kldyWak/LbUUKXZkSsYJ

    (Sorry for the search url; couldn't figure out how to clean it. I wish
    Google Groups added a permalink to each outbound email. :/)

    -josh


    On Wed, Jul 31, 2013 at 9:53 AM, Jochen Voss wrote:

    Dear all,

    Here is a question about channels: Is it possible to use a select
    statement
    to wait until a receiver on a channel is ready, and only then to compute
    the
    value to send?

    Example: is it possible to set up a "chan time.Time" such that reading
    the
    channel gives the current time? I tried the following:

    c := make(chan time.Time)
    go func() {
    for {
    select {
    case c <- time.Now():
    // I would like for time.Now() to be evaluated only when a reader is
    ready.
    }
    }
    }()

    This code doesn't work; reading from c returns the time of the previous
    read
    (see http://play.golang.org/p/o1o8jvmtdf for illustration). Is there a
    way to
    modify the code to get the current time?

    (This is only an example; in my real problem, data is accumulated over
    time and the channel should return a hash of the data accumulated so
    far.)

    Many thanks,
    Jochen
    --
    http://seehuhn.de/

    --
    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.
  • Jeff Juozapaitis at Aug 1, 2013 at 2:37 am
    I was assuming time.Now() was just an example, but you're right.

    On Wed, Jul 31, 2013 at 7:27 PM, Carlos Castillo wrote:

    Well, technically, in that scenario, you could just evaluate the function
    once you got a signal from the channel:

    timeout := time.After(5 * time.Milliseconds)
    <-timeout
    x := time.Now()

    On Wednesday, July 31, 2013 12:38:28 PM UTC-7, Jsor wrote:

    What about sending a function, and evaluating it upon receive? This isn't
    perfectly equivalent, and may not work in some cases, but if your only
    concern is evaluating right when the reader is ready to deal with it it
    should work. See:

    http://play.golang.org/p/**gMXPFpZDXW<http://play.golang.org/p/gMXPFpZDXW>

    On Wednesday, July 31, 2013 12:31:01 PM UTC-7, Joshua Bleecher Snyder
    wrote:
    In addition to the other replies, see this thread:
    https://groups.google.**com/forum/#!searchin/golang-**
    nuts/lazy/golang-nuts/**qZO4kldyWak/LbUUKXZkSsYJ<https://groups.google.com/forum/#!searchin/golang-nuts/lazy/golang-nuts/qZO4kldyWak/LbUUKXZkSsYJ>

    (Sorry for the search url; couldn't figure out how to clean it. I wish
    Google Groups added a permalink to each outbound email. :/)

    -josh


    On Wed, Jul 31, 2013 at 9:53 AM, Jochen Voss wrote:

    Dear all,

    Here is a question about channels: Is it possible to use a select
    statement
    to wait until a receiver on a channel is ready, and only then to
    compute the
    value to send?

    Example: is it possible to set up a "chan time.Time" such that reading
    the
    channel gives the current time? I tried the following:

    c := make(chan time.Time)
    go func() {
    for {
    select {
    case c <- time.Now():
    // I would like for time.Now() to be evaluated only when a reader is
    ready.
    }
    }
    }()

    This code doesn't work; reading from c returns the time of the previous
    read
    (see http://play.golang.org/p/**o1o8jvmtdf<http://play.golang.org/p/o1o8jvmtdf>for illustration). Is there a way to
    modify the code to get the current time?

    (This is only an example; in my real problem, data is accumulated over
    time and the channel should return a hash of the data accumulated so
    far.)

    Many thanks,
    Jochen
    --
    http://seehuhn.de/

    --
    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<https://groups.google.com/groups/opt_out>
    .

    --
    You received this message because you are subscribed to a topic in the
    Google Groups "golang-nuts" group.
    To unsubscribe from this topic, visit
    https://groups.google.com/d/topic/golang-nuts/_WfgNq1nDJQ/unsubscribe.
    To unsubscribe from this group and all its topics, 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.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJul 31, '13 at 4:53p
activeAug 1, '13 at 2:37a
posts10
users6
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase