FAQ
This example returns no output.

package main

import (
"fmt"
)

func main(){

c:=make(chan int) //create un-buffered channel of type int

ints:=[]int{1,2,3,4,5} //slice of ints for testing

go func(){ //kick start goroutitne that sends to the
channel and finally closes it
for _,v:=range ints{
c<-v
}
close(c)
}()

go func() { //kick start goroutine that recieves from the
channel and prints the ints
for v:=range c{
fmt.Println(v)
}
}()

}

But, take away the go keyword from the second goroutine; thus letting to
function run the main goroutine. You get see the prints as expected.

package main

import (
"fmt"
)

func main(){

c:=make(chan int) //create unbuffered chanel of type int

ints:=[]int{1,2,3,4,5} //slice of ints for testing

go func(){ //kick start goroutitne that sends to the chanel
and finally closes it
for _,v:=range ints{
c<-v
}
close(c)
}()

func() { //call function without start gooutine gets
output as expected.
for v:=range c{
fmt.Println(v)
}
}()

}

Now consider this. Similar to the first example with two goroutines but
added another dummy channel that the fist goroutine sends a dummy value and
the main goroutine finally receives it.

package main

import (
"fmt"
)

func main(){

c:=make(chan int) //create unbuffered channel of type int

d:=make(chan int) //dummy unbuffered channel for testing

ints:=[]int{1,2,3,4,5} //slice of ints for testing

go func(){ //kick start goroutitne that sends to the
channel and finally closes it
for _,v:=range ints{
c<-v
}
close(c)

d<-9 //send dummy value to dummy channel and closes it
close(d)
}()

go func() { //kick start goroutine that recieves from the
channel and prints the ints
for v:=range c{
fmt.Println(v)
}
}()

fmt.Println(<-d) //printout dummy value in main goroutine and
vuala!
//you see all the prints from the second
goroutine as well.
}

There is something connected to recieving from a channel in the main
goroutine and seeing prints within goroutines.
Can someone explain this please.


--

Search Discussions

  • David Symonds at Nov 18, 2012 at 5:33 am
    A Go program terminates once the main function returns. In your first
    program there is nothing stopping main from returning, so the
    goroutines don't get a chance to run.

    --
  • Dave Cheney at Nov 18, 2012 at 5:34 am
    You can use select {} to make you main function sleep forever without
    consuming CPU.
    On 18 Nov 2012 16:33, "David Symonds" wrote:

    A Go program terminates once the main function returns. In your first
    program there is nothing stopping main from returning, so the
    goroutines don't get a chance to run.

    --

    --
  • Minux at Nov 18, 2012 at 6:47 am
    you need proper synchronization the main goroutine's exit with
    the finish of other goroutines.

    one way to do is to use sync.WaitGroup, see the example in its
    docs for its usage.
    On Sunday, November 18, 2012, Gomain Hoyes wrote:

    This example returns no output.

    package main

    import (
    "fmt"
    )

    func main(){

    c:=make(chan int) //create un-buffered channel of type int

    ints:=[]int{1,2,3,4,5} //slice of ints for testing

    go func(){ //kick start goroutitne that sends to the
    channel and finally closes it
    for _,v:=range ints{
    c<-v
    }
    close(c)
    }()

    go func() { //kick start goroutine that recieves from the
    channel and prints the ints
    for v:=range c{
    fmt.Println(v)
    }
    }()

    }

    But, take away the go keyword from the second goroutine; thus letting to
    function run the main goroutine. You get see the prints as expected.

    package main

    import (
    "fmt"
    )

    func main(){

    c:=make(chan int) //create unbuffered chanel of type int

    ints:=[]int{1,2,3,4,5} //slice of ints for testing

    go func(){ //kick start goroutitne that sends to the
    chanel and finally closes it
    for _,v:=range ints{
    c<-v
    }
    close(c)
    }()

    func() { //call function without start gooutine gets
    output as expected.
    for v:=range c{
    fmt.Println(v)
    }
    }()

    }

    Now consider this. Similar to the first example with two goroutines but
    added another dummy channel that the fist goroutine sends a dummy value and
    the main goroutine finally receives it.

    package main

    import (
    "fmt"
    )

    func main(){

    c:=make(chan int) //create unbuffered channel of type int

    d:=make(chan int) //dummy unbuffered channel for testing

    ints:=[]int{1,2,3,4,5} //slice of ints for testing

    go func(){ //kick start goroutitne that sends to the
    channel and finally closes it
    for _,v:=range ints{
    c<-v
    }
    close(c)

    d<-9 //send dummy value to dummy channel and closes
    it
    close(d)
    }()

    go func() { //kick start goroutine that recieves from the
    channel and prints the ints
    for v:=range c{
    fmt.Println(v)
    }
    }()

    fmt.Println(<-d) //printout dummy value in main goroutine and
    vuala!
    //you see all the prints from the second
    goroutine as well.
    }

    There is something connected to recieving from a channel in the main
    goroutine and seeing prints within goroutines.
    Can someone explain this please.


    --

    --
  • Gomain Hoyes at Nov 18, 2012 at 7:19 am
    Thanks guys.. it's all about timing!
    On Sunday, November 18, 2012 11:10:24 AM UTC+7, Gomain Hoyes wrote:

    This example returns no output.

    package main

    import (
    "fmt"
    )

    func main(){

    c:=make(chan int) //create un-buffered channel of type int

    ints:=[]int{1,2,3,4,5} //slice of ints for testing

    go func(){ //kick start goroutitne that sends to the
    channel and finally closes it
    for _,v:=range ints{
    c<-v
    }
    close(c)
    }()

    go func() { //kick start goroutine that recieves from the
    channel and prints the ints
    for v:=range c{
    fmt.Println(v)
    }
    }()

    }

    But, take away the go keyword from the second goroutine; thus letting to
    function run the main goroutine. You get see the prints as expected.

    package main

    import (
    "fmt"
    )

    func main(){

    c:=make(chan int) //create unbuffered chanel of type int

    ints:=[]int{1,2,3,4,5} //slice of ints for testing

    go func(){ //kick start goroutitne that sends to the
    chanel and finally closes it
    for _,v:=range ints{
    c<-v
    }
    close(c)
    }()

    func() { //call function without start gooutine gets
    output as expected.
    for v:=range c{
    fmt.Println(v)
    }
    }()

    }

    Now consider this. Similar to the first example with two goroutines but
    added another dummy channel that the fist goroutine sends a dummy value and
    the main goroutine finally receives it.

    package main

    import (
    "fmt"
    )

    func main(){

    c:=make(chan int) //create unbuffered channel of type int

    d:=make(chan int) //dummy unbuffered channel for testing

    ints:=[]int{1,2,3,4,5} //slice of ints for testing

    go func(){ //kick start goroutitne that sends to the
    channel and finally closes it
    for _,v:=range ints{
    c<-v
    }
    close(c)

    d<-9 //send dummy value to dummy channel and closes
    it
    close(d)
    }()

    go func() { //kick start goroutine that recieves from the
    channel and prints the ints
    for v:=range c{
    fmt.Println(v)
    }
    }()

    fmt.Println(<-d) //printout dummy value in main goroutine and
    vuala!
    //you see all the prints from the second
    goroutine as well.
    }

    There is something connected to recieving from a channel in the main
    goroutine and seeing prints within goroutines.
    Can someone explain this please.

    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedNov 18, '12 at 5:31a
activeNov 18, '12 at 7:19a
posts5
users4
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase