I’m new to golang, I was going through the goroutines subject and I would like to create variadic merger of channels. Here is a on-line compiler A Tour of Go and here is the code I wrote:
package main
import "fmt"
func generate(v int) <- chan int {
ch := make(chan int, 1)
go func(){
for i := v; ; i++ {
ch <- i
}
}()
return ch
}
func merge(v ... <-chan int ) <- chan int {
ch := make( chan int, 1 )
for z := range v {
go func() { for { ch <- <-z } }()
}
return ch
}
func main() {
ch := merge( generate(1000), generate(100), generate(100000) )
for i := 0; i < 10; i++ {
fmt.Println(<- ch)
}
}
As a response I get
# example
./prog.go:20:27: invalid operation: <-z (receive from non-chan type int)
Why it is? Shouldn’t go recognise that I’m going through the channels in a for loop?