I am trying to use Go feeds and are confused with the below example function from the 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 }
Home:
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) }
Completion Code: http://play.golang.org/p/Qh30wzo4m0
My doubts: 1.My understanding was, as soon as the return is called a function, it will be stopped, and the channel inside this function will no longer have life.Here the return statement is called only once. But the contents of the Out channel are received many times. In this case, how is the actual thread of execution? I am new to parallel programming. Don't forget to help
source share