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:
My understanding was, once return
is called the function will be terminated and the channel inside that function has no more life.
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.)
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 gettingout
channel as return fromgen()
function.
How it is possible to get it aftergen()
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 theout
channel, which makes it not garbage collected.
It doesn't matter if thegen()
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With