Range over array of channel

Issue

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

Solution

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

Answered By – chmike

Answer Checked By – Terry (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.