FAQ
I was reading http://blog.golang.org/pipelines, the concurrency patterns. I
have following doubt

How can function sq in line 6 read again from the channel "in" ? I thought
its already read a line before. If yes how can we say the sq work is
distributed across 2 go routines ? I think I am missing some core concept
here. Please help me to understand the code below


1 func main() {2 in := gen(2, 3)34 // Distribute the sq work across two goroutines that both read from in.5 c1 := sq(in)6 c2 := sq(in)78 // Consume the merged output from c1 and c2.9 for n := range merge(c1, c2) {10 fmt.Println(n) // 4 then 9, or 9 then 411 }

12 }

Thanks,
Anumodh




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

  • Ian Lance Taylor at Oct 9, 2014 at 2:34 pm

    On Thu, Oct 9, 2014 at 3:10 AM, wrote:
    I was reading http://blog.golang.org/pipelines, the concurrency patterns. I
    have following doubt

    How can function sq in line 6 read again from the channel "in" ? I thought
    its already read a line before. If yes how can we say the sq work is
    distributed across 2 go routines ? I think I am missing some core concept
    here. Please help me to understand the code below
    The sq function starts a goroutine to read from the channel it is
    given. The sq function does not itself read from the channel.
    Calling sq twice means that two goroutines start reading from the
    channel. The gen function started a goroutine to write a stream of
    values to the channel. So there are two goroutines reading from the
    channel and one goroutine writing to it. (Which of the two reading
    goroutines will receive a particular value is unpredictable, and the
    order in which they write to the output channel is unpredictable, so
    the order of the output is unpredictable.)

    Ian

    1 func main() {
    2 in := gen(2, 3)
    3
    4 // Distribute the sq work across two goroutines that both read from in.
    5 c1 := sq(in)
    6 c2 := sq(in)
    7
    8 // Consume the merged output from c1 and c2.
    9 for n := range merge(c1, c2) {
    10 fmt.Println(n) // 4 then 9, or 9 then 4
    11 }

    12 }
    --
    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.
  • Anaray at Oct 9, 2014 at 5:50 pm
    Thanks Ian . I got it now.
    On Thursday, October 9, 2014 8:04:36 PM UTC+5:30, Ian Lance Taylor wrote:
    On Thu, Oct 9, 2014 at 3:10 AM, <anu...@gmail.com <javascript:>> wrote:
    I was reading http://blog.golang.org/pipelines, the concurrency
    patterns. I
    have following doubt

    How can function sq in line 6 read again from the channel "in" ? I thought
    its already read a line before. If yes how can we say the sq work is
    distributed across 2 go routines ? I think I am missing some core concept
    here. Please help me to understand the code below
    The sq function starts a goroutine to read from the channel it is
    given. The sq function does not itself read from the channel.
    Calling sq twice means that two goroutines start reading from the
    channel. The gen function started a goroutine to write a stream of
    values to the channel. So there are two goroutines reading from the
    channel and one goroutine writing to it. (Which of the two reading
    goroutines will receive a particular value is unpredictable, and the
    order in which they write to the output channel is unpredictable, so
    the order of the output is unpredictable.)

    Ian

    1 func main() {
    2 in := gen(2, 3)
    3
    4 // Distribute the sq work across two goroutines that both read from in.
    5 c1 := sq(in)
    6 c2 := sq(in)
    7
    8 // Consume the merged output from c1 and c2.
    9 for n := range merge(c1, c2) {
    10 fmt.Println(n) // 4 then 9, or 9 then 4
    11 }

    12 }
    --
    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
postedOct 9, '14 at 1:58p
activeOct 9, '14 at 5:50p
posts3
users2
websitegolang.org

2 users in discussion

Anaray: 2 posts Ian Lance Taylor: 1 post

People

Translate

site design / logo © 2022 Grokbase