如何分配一个通道数组

huangapple go评论157阅读模式
英文:

How to allocate an array of channels

问题

如何创建一个通道数组

**例如:**用一个大小为5的通道数组替换以下五行代码:

  1. var c0 chan int = make(chan int);
  2. var c1 chan int = make(chan int);
  3. var c2 chan int = make(chan int);
  4. var c3 chan int = make(chan int);
  5. var c4 chan int = make(chan int);
英文:

How to create an array of channels?

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

  1. var c0 chan int = make(chan int);
  2. var c1 chan int = make(chan int);
  3. var c2 chan int = make(chan int);
  4. var c3 chan int = make(chan int);
  5. var c4 chan int = make(chan int);

答案1

得分: 77

语句var chans [5]chan int将分配一个大小为5的数组,但所有的通道都将是nil

一种方法是使用切片字面量:

  1. var chans = []chan int {
  2. make(chan int),
  3. make(chan int),
  4. make(chan int),
  5. make(chan int),
  6. make(chan int),
  7. }

如果你不想重复自己,你需要遍历它并初始化每个元素:

  1. var chans [5]chan int
  2. for i := range chans {
  3. chans[i] = make(chan int)
  4. }
英文:

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:

  1. var chans = []chan int {
  2. make(chan int),
  3. make(chan int),
  4. make(chan int),
  5. make(chan int),
  6. make(chan int),
  7. }

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

  1. var chans [5]chan int
  2. for i := range chans {
  3. chans[i] = make(chan int)
  4. }

答案2

得分: 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)
}
你可以这样创建,使用切片和通道

英文:
  1. c := make(map[int]chan int)
  2. for i := 1; i &lt;= 5; i++ {
  3. c[i] = make(chan int)
  4. }
  5. for _,v := range c {
  6. fmt.Println(v)
  7. }

You can create like that, use slice and channel

答案3

得分: 0

[]chan[]string的示例。它可以扩展到所有类型的情况。

  1. package main
  2. import (
  3. "fmt"
  4. "sync"
  5. )
  6. func main() {
  7. var ch [4]chan []string
  8. for i := range ch {
  9. ch[i] = make(chan []string, 1)
  10. }
  11. ch1 := []string{"this", "that", "who"}
  12. ch2 := []string{"one", "two", "three"}
  13. ch3 := []string{"four", "five", "six"}
  14. ch4 := []string{"seven", "eight", "nine"}
  15. wg := sync.WaitGroup{}
  16. wg.Add(1)
  17. for i := 0; i < 4; i++ {
  18. switch i {
  19. case 0:
  20. ch[i] <- ch1
  21. case 1:
  22. ch[i] <- ch2
  23. case 2:
  24. ch[i] <- ch3
  25. case 3:
  26. ch[i] <- ch4
  27. default:
  28. }
  29. }
  30. wg.Done()
  31. for i := 0; i < 4; i++ {
  32. fmt.Println(<-ch[i])
  33. }
  34. wg.Wait()
  35. }
英文:

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

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;sync&quot;
  5. )
  6. func main() {
  7. var ch [4]chan []string
  8. for i := range ch {
  9. ch[i] = make(chan []string, 1)
  10. }
  11. ch1 := []string{&quot;this&quot;, &quot;that&quot;, &quot;who&quot;}
  12. ch2 := []string{&quot;one&quot;, &quot;two&quot;, &quot;three&quot;}
  13. ch3 := []string{&quot;four&quot;, &quot;five&quot;, &quot;six&quot;}
  14. ch4 := []string{&quot;seven&quot;, &quot;eight&quot;, &quot;nine&quot;}
  15. wg := sync.WaitGroup{}
  16. wg.Add(1)
  17. for i := 0; i &lt; 4; i++ {
  18. switch i {
  19. case 0:
  20. ch[i] &lt;- ch1
  21. case 1:
  22. ch[i] &lt;- ch2
  23. case 2:
  24. ch[i] &lt;- ch3
  25. case 3:
  26. ch[i] &lt;- ch4
  27. default:
  28. }
  29. }
  30. wg.Done()
  31. for i := 0; i &lt; 4; i++ {
  32. fmt.Println(&lt;-ch[i])
  33. }
  34. wg.Wait()
  35. }

答案4

得分: 0

如果是可变长度数组,在make([]chan int, N)之后使用for循环进行初始化。

  1. c := make([]chan int, 5) // [nil, nil, nil, nil, nil] (尚未初始化)
  2. for i := 0; i < len(c); i++ {
  3. c[i] = make(chan int)
  4. }
英文:

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

  1. c := make([]chan int, 5) // [nil, nil, nil, nil, nil] (doesn&#39;t initialize yet)
  2. for i := 0; i &lt; len(c); i++ {
  3. c[i] = make(chan int)
  4. }

答案5

得分: -5

我认为在这种情况下,你可以使用带缓冲的通道。

通道可以被缓冲。通过将缓冲长度作为第二个参数传递给 make 函数来初始化一个带缓冲的通道:

  1. ch := make(chan int, 5)

当缓冲区满时,向带缓冲的通道发送数据会阻塞。当缓冲区为空时,接收数据会阻塞。

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

英文:

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:

  1. 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

huangapple
  • 本文由 发表于 2010年5月24日 02:56:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/2893004.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定