FAQ
Basically, I am trying out channel, one with buffer and one with no buffer.

In both cases, the channel gets closed before range, as it should be. But
when I retrieve from the closed channel, I get different output from each
case, since the buffered channel is not closed at some point but the
unbuffered channel is closed all the time I check the value. I want to know
when exactly the channel gets closed in buffered case.

It's a bit hard to explain in words. Please run the following playground
and you will see what is making it hard to understand.

*http://play.golang.org/p/vRECrMcIZx*

I am asking myself some question to understand this code
and can't answer the problem 2 and 3.

---

*[Problem #2]*
Then why the NonBuffered never outputs the TRUE value?

Does this mean the unbuffered channel is always closed?
Then how come the unbuffered channel still produce the right outcome of
fibonacci calculation?

---

*[Problem #3]*
Why the NonBuffered output gets cut in the middle like the following?

0 1 1 2 NON_Buffered value: 0
NON_Buffered ok: false
3

It should be something related with goroutine but Why in this way?

---

I also tried to look at the source code of close but can't find the
implementation

*http://golang.org/src/pkg/builtin/builtin.go?h=close#L213*

Just one line for close function?



Thanks a lot!

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

  • Roger peppe at Dec 18, 2013 at 10:22 am

    On 18 December 2013 09:05, Gyu-Ho Lee wrote:
    Basically, I am trying out channel, one with buffer and one with no buffer.

    In both cases, the channel gets closed before range, as it should be. But
    when I retrieve from the closed channel, I get different output from each
    case, since the buffered channel is not closed at some point but the
    unbuffered channel is closed all the time I check the value. I want to know
    when exactly the channel gets closed in buffered case.
    You can't tell if a buffered channel has been closed without
    consuming all the values inside it, otherwise those values would be discarded
    because there would be no way to read them.

    If you want to know immediately about something, you'll
    need to use a side channel.

    Rather than inspecting the source code, it's probably better to check
    the spec for this behaviour. See http://golang.org/ref/spec#Close

    In particular:

    "After calling close, and after any previously sent values have been
    received, receive operations will return the zero value for the
    channel's type without blocking."

       cheers,
         rog.

    --
    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.
  • Chris dollin at Dec 18, 2013 at 10:33 am

    On 18 December 2013 09:05, Gyu-Ho Lee wrote:
    I also tried to look at the source code of close but can't find the
    implementation

    *http://golang.org/src/pkg/builtin/builtin.go?h=close#L213
    <http://golang.org/src/pkg/builtin/builtin.go?h=close#L213>*

    Just one line for close function?
    As that page says (at the top)

    Package builtin provides documentation for Go's predeclared
    identifiers.The items documented here are not actually in package
    builtin

    The code for Close() is inside the (written-in-C) runtime support. As Roger
    says, you should be checking what the spec says about Close() rather
    than peering at the implementation if you want to understand what they are,

    Chris

    --
    Chris "allusive" Dollin

    --
    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.
  • Ian Lance Taylor at Dec 18, 2013 at 2:24 pm

    On Wed, Dec 18, 2013 at 1:05 AM, Gyu-Ho Lee wrote:
    I also tried to look at the source code of close but can't find the
    implementation

    http://golang.org/src/pkg/builtin/builtin.go?h=close#L213

    Just one line for close function?
    For the record the source code for close is in pkg/runtime/chan.c.
    It's the function runtime·closechan.

    Ian

    --
    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.
  • Gyu-Ho Lee at Dec 19, 2013 at 4:22 am
    Thanks a lot!
    On Wednesday, December 18, 2013 6:23:58 AM UTC-8, Ian Lance Taylor wrote:
    On Wed, Dec 18, 2013 at 1:05 AM, Gyu-Ho Lee wrote:

    I also tried to look at the source code of close but can't find the
    implementation

    http://golang.org/src/pkg/builtin/builtin.go?h=close#L213

    Just one line for close function?
    For the record the source code for close is in pkg/runtime/chan.c.
    It's the function runtime·closechan.

    Ian
    --
    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
postedDec 18, '13 at 9:05a
activeDec 19, '13 at 4:22a
posts5
users4
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase