I can range over channel to get the value from it, but when I try to read from the array of channels it gives me an error.
All I am doing is creating array of channel that contain 2 channel, first one contain values from 1 to 5 and the second one have values from 6 to 10 and I just want to read the values from the array of channel itself.
Here is my code :
package main
import "fmt"
func main() {
channelList := make([]chan int, 2)
for i := 0; i < 1; i++ {
channelList = append(channelList, make(chan int))
}
FChannel := make(chan int, 5)
for i := 1; i <= 5; i++ {
FChannel <- i // contain values from 1 to 5
}
close(FChannel)
channelList = append(channelList, FChannel)
SChannel := make(chan int, 5)
for j := 6; j <= 10; j++ {
SChannel <- j // contain values from 6 to 10
}
close(SChannel)
channelList = append(channelList, SChannel)
for _, c := range channelList {
for range c {
fmt.Println(<-c)
}
}
}
but it gives me this error :
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [chan receive (nil chan)]: main.main()
for rance c {
should probably also befor v := range c
where you then use v as the read value. Ranging over your channel will produce all values of that channel until you reach the end (channel closed) . I was surprised that even compiled :D – pandaadb