19

I'm trying with Go channels and confused with below function example from go blog:

func gen(nums []int) <-chan int {
    out := make(chan int)
    go func() {
        for _, n := range nums {
            out <- n
        }
        close(out)
    }()
    fmt.Println("return statement is called ")
    return out
}

Main :

func main() {
    c := make(chan int)
    c = gen([]int{2, 3, 4, 5})

    // Consume the output.
    // Print 2,3,4,5
    fmt.Println(<-c)
    fmt.Println(<-c)
    fmt.Println(<-c)
    fmt.Println(<-c)
}

Complete Code: http://play.golang.org/p/Qh30wzo4m0

My Doubts:

  1. My understanding was, once return is called the function will be terminated and the channel inside that function has no more life.

  2. The return statement is called only once. But the content of the out channel is read many times. In this case what is the actual flow of execution?

(I'm new to concurrent programming.)

bain
  • 1,710
  • 14
  • 15
faisal_kk
  • 1,039
  • 1
  • 11
  • 19

3 Answers3

19
out := make(chan int)

This is not a buffered channel, which means the out <- n will block until someone somewhere reads that channel (the fmt.Println(<-c) calls)
(See also "do golang channels maintain order")

So the return at the end of the gen() function doesn't mean the literal go func() is terminated (since it is still waiting for readers to consume the content of the out channel).

But main function getting out channel as return from gen() function.
How it is possible to get it after gen() is terminated?

The fact that gen() terminates has no effect on its returned value (the out channel): the goal of "gen()" is to "generate" that out channel.

main can use out (as the returned value of gen()) long after gen() terminates.

The literal go func within gen() still runs, even if gen() is terminated.


As noted by vagabond in the comments:

When gen() returns, there is still a reference to the out channel, which makes it not garbage collected.
It doesn't matter if the gen() has a go routine closure is using the channel.

When a channel is not used, the sender can close the channel explicitly.
And then the select for the channel will make the go routine to exit.
At last, everything will be cleared.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • ,Thanks for quick reply.I got your point,But out channel is passed to main function through return value of gen() function.So when gen() is terminated How it is possible ? – faisal_kk Feb 19 '15 at 09:27
  • 1
    gen may be terminated, but its internal literal go func remains, as an independent goroutine. – VonC Feb 19 '15 at 09:28
  • variable 'c' in main is return of gen() function.So what you are saying is ,It will be populated with values even after gen() terminated due to live go routine inside gen() ... – faisal_kk Feb 19 '15 at 09:35
  • @faisalkk yes, since the go func is a goroutine which will run asynchronously, even if gen() terminates. I have edited the answer. – VonC Feb 19 '15 at 09:40
  • when gen() returns, there is still a reference to the out channel, which makes it not garbage collected. it doesn't matter if the gen() has a go routine closure is using the channel. – zztczcx Sep 16 '21 at 04:09
  • @vagabond I agree. Is there any way to avoid this? – VonC Sep 16 '21 at 05:22
  • @VonC, normally you don't need to care about garbage collection, because go will automatically do it. when a channel is not used, the sender can close the channel explicitly. and then the select for the channel will make the go routine to exit. at last everything will be cleared. – zztczcx Sep 16 '21 at 06:35
  • @vagabond Thank you. I have included your comment in the answer for more visibility. – VonC Sep 16 '21 at 06:53
4

Because gen() fires off the channel population function as a goroutine;

go func() {
    for _, n := range nums {
        out <- n
    }
    close(out)
}()

and it blocks when the first value is sent on the out channel, because nothing is receiving yet (unbuffered channels block on sending until something receives on them), that goroutine doesn't end when the gen() function returns.

The receives from c in main()

fmt.Println(<-c)
...

then cause the goroutine started in gen() to keep populating the channel as the results are read out, and then finally main() returns when the goroutine returns, because there is nothing left to send on out, and nothing left to receive on c.

Also, the c := make(<-chan int) in main() is unnecessary as gen() creates a channel and returns it.

See Playground

Intermernet
  • 18,604
  • 4
  • 49
  • 61
  • @Internet I'm clear on your point :" goroutine doesn't end when the gen() function returns." But main function getting out channel as return from gen() function .How it is possible to get it after gen() is terminated?That is my doubt...sorry if I;m asking stupid questions,I'm new to this... – faisal_kk Feb 19 '15 at 09:32
  • @faisalkk the `gen()` function returns the channel, then ends. The channel isn't "owned" by that function, it's just created and returned (For illustration, the goroutine can be seen as a separate "background" function, that is "disowned" by the use of the `go` keyword.). At that point the `gen()` function is free to end, and the channel can be used by other functions. – Intermernet Feb 19 '15 at 11:29
-2

I think this is what you want: https://play.golang.org/p/WSBUPPyQiY6

package main

import (
    "fmt"
    "sync"
    "time"
)

func read() (elemChan chan int) {
    elemChan = make(chan int)
    go func() {
        for k := 0; k < 1000; k++ {
            elemChan <- k
        }
        close(elemChan)
    }()
    return
}

func main() {
    fmt.Println("Hello, playground")
    elemChan := read()
    wg := sync.WaitGroup{}
    for k := 0; k < 2; k++ {
        wg.Add(1)
        go func(k int) {
            for {
                e, more := <-elemChan
                if !more {
                    wg.Done()
                    return
                }
                fmt.Printf("goroutine #%d: %d\n", k, e)
                time.Sleep(1000 * time.Nanosecond)
            }
        }(k)
    }
    wg.Wait()
}
Grana
  • 7
  • 2
  • Actually that was a doubt about how parallel/concurrent flow of execution happen and it was answered well. Please add enough comments or explanation instead of pasting code snippets here. – faisal_kk Feb 17 '20 at 11:48
  • I believe a code example showing how to return an open channel for other consumers to use can be helpful for the community. Other users in this thread have thoroughly commented and answered the questions, and I think the only thing that was still missing was a working example. – Grana Feb 17 '20 at 15:27