0
votes

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()

1
I think your issue is that you create a slice with length 2, which will have to 0 values in the beginning, and then append to it. Subsequently when you start ranging over your channels, you will first hit the channels taht you created in the first 3 lines (but never closed or wrote anything into). So that way the range over c in the end will never read anything and deadlocks the main thread. Try creating the slice of channels as an empty slice and append to it. I.e. a null slice (zero value of a slice) can be appended and will be expanded automatically. That way your code can runpandaadb
your final for rance c { should probably also be for 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 :Dpandaadb
yes that solves it thank youomar

1 Answers

0
votes

It's because you first create two channels with no buffering and no values. You then append two channels with buffering and values.

When you range over the channels, you read from the first empty channel, and this blocks forever.

EDIT

Here is the cleaned code:

package main

import "fmt"

func main() {
    channelList := make([]chan int, 0, 2)

    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 v := range c {
            fmt.Println(v)
        }
    }
}

The output I get is

1
2
3
4
5
6
7
8
9
10