FAQ
Hi all,

Say a goroutine is blocking on a channel read. Is there any way to end its
execution without its intervention?

I want to implement a (toy, don't freak out) try-catch mechanism. I want
the catch block to be able to resume execution, or end it.

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

I could do it just making "try" a function that returns nothing and
panicking from the "catch" function to end the execution, but I find it
inelegant (and messy if the "try" function recovers).

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

  • Patrick Mylund Nielsen at Mar 27, 2013 at 10:51 am
    No -- you should use select to read from that channel or e.g. a timeout
    channel instead, e.g.:

    select {
    v := <-ch:
    ...
    <-time.After(5 * time.Second):
    // timed out
    ...
    }

    On Wed, Mar 27, 2013 at 11:48 AM, Toni Cárdenas wrote:

    Hi all,

    Say a goroutine is blocking on a channel read. Is there any way to end its
    execution without its intervention?

    I want to implement a (toy, don't freak out) try-catch mechanism. I want
    the catch block to be able to resume execution, or end it.

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

    I could do it just making "try" a function that returns nothing and
    panicking from the "catch" function to end the execution, but I find it
    inelegant (and messy if the "try" function recovers).

    --
    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.
  • Jan Mercl at Mar 27, 2013 at 10:52 am

    On Wed, Mar 27, 2013 at 11:48 AM, Toni Cárdenas wrote:
    Hi all,

    Say a goroutine is blocking on a channel read. Is there any way to end its
    execution without its intervention?
    Closing the channel will unblock the goroutine. If the read was of the
    form 'v, ok = <-c', then ok will be false. If the goroutine returns
    for ok == false then you can force its termination by closing the
    channel.

    -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.
  • Toni Cárdenas at Mar 27, 2013 at 11:47 am
    Both approaches (if I understand correctly) require overhead within the try
    block and are not much better (i.e. lazier and quicker) than handling the
    error Go-style.

    I went with a panic based solution: http://play.golang.org/p/PlyhlUB_CQ Any
    thoughts?
    On Wednesday, March 27, 2013 11:51:51 AM UTC+1, Jan Mercl wrote:
    On Wed, Mar 27, 2013 at 11:48 AM, Toni Cárdenas wrote:
    Hi all,

    Say a goroutine is blocking on a channel read. Is there any way to end its
    execution without its intervention?
    Closing the channel will unblock the goroutine. If the read was of the
    form 'v, ok = <-c', then ok will be false. If the goroutine returns
    for ok == false then you can force its termination by closing the
    channel.

    -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
postedMar 27, '13 at 10:48a
activeMar 27, '13 at 11:47a
posts4
users3
websitegolang.org

People

Translate

site design / logo © 2020 Grokbase