35

How to create an array of channels?

For example: replace the following five lines with an array of channels, with a size of 5:

var c0 chan int = make(chan int);
var c1 chan int = make(chan int);
var c2 chan int = make(chan int);
var c3 chan int = make(chan int);
var c4 chan int = make(chan int);

5 Answers 5

77

The statement var chans [5]chan int would allocate an array of size 5, but all the channels would be nil.

One way would be to use a slice literal:

var chans = []chan int {
   make(chan int),
   make(chan int),
   make(chan int),
   make(chan int),
   make(chan int),
}

If you don't want to repeat yourself, you would have to iterate over it and initialize each element:

var chans [5]chan int
for i := range chans {
   chans[i] = make(chan int)
}
3
  • Why is your solution better than Chickencha's succinct and idiomatic chans := make([]chan int, 5)?
    – peterSO
    Jun 2, 2010 at 14:32
  • 23
    If the channels are not explicitly allocated with make(chan int), they will default to nil. Jun 2, 2010 at 14:59
  • This isn't a terrible option if you know in advance the number of channels you require. Jul 5, 2022 at 9:25
2
c := make(map[int]chan int)
for i := 1; i <= 5; i++ {
    c[i] = make(chan int)
}
for _,v := range c {
    fmt.Println(v)
}

You can create like that, use slice and channel

0

Example for []chan[]string. it can be extended for all type of cases.

package main

import (
    "fmt"
    "sync"
)

func main() {
    var ch [4]chan []string
    for i := range ch {
        ch[i] = make(chan []string, 1)
    }

    ch1 := []string{"this", "that", "who"}

    ch2 := []string{"one", "two", "three"}

    ch3 := []string{"four", "five", "six"}

    ch4 := []string{"seven", "eight", "nine"}

    wg := sync.WaitGroup{}

    wg.Add(1)
    for i := 0; i < 4; i++ {
        switch i {
        case 0:
            ch[i] <- ch1
        case 1:
            ch[i] <- ch2
        case 2:
            ch[i] <- ch3
        case 3:
            ch[i] <- ch4
        default:
        }
    }
    wg.Done()

    for i := 0; i < 4; i++ {
        fmt.Println(<-ch[i])

    }
    wg.Wait()
}
1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Oct 27, 2021 at 10:59
0

If variable length array, initialize with for loop after make([]chan int, N).

c := make([]chan int, 5) // [nil, nil, nil, nil, nil] (doesn't initialize yet)
for i := 0; i < len(c); i++ {
    c[i] = make(chan int)
}
-5

I think you can use buffered channels in this case.

Channels can be buffered. Provide the buffer length as the second argument to make to initialize a buffered channel:

ch := make(chan int, 5)

Sends to a buffered channel block only when the buffer is full. Receives block when the buffer is empty.

https://tour.golang.org/concurrency/3

1
  • 4
    An array can be filled in your order, an buffered channel is just something different
    – Hoffmann
    Dec 3, 2018 at 23:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.