简体   繁体   中英

Create a slice of buffered channel in golang

I couldn't find a way to create a slice of buffered channels in golang. I know how to create slice of unbuffered channel given as below

type ch chan int
channels := make([]ch,5)

This statement channels := make([]ch,5) is simply allocating the container (the slice of channels which has a length of 5). In addition to that you have to initialize each channel individually which is when you would declare them as buffered rather than unbuffered. So extending your example just do this:

for i, _ := range channels {
     channels[i] = make(chan int, BufferSize)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM