如何分配一个通道数组

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

How to allocate an array of channels

问题

如何创建一个通道数组

**例如:**用一个大小为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);
英文:

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

答案1

得分: 77

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

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

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

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

var chans [5]chan int
for i := range chans {
   chans[i] = make(chan int)
}
英文:

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

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

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

You can create like that, use slice and channel

答案3

得分: 0

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

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()
}
英文:

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

package main

import (
	&quot;fmt&quot;
	&quot;sync&quot;
)

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

	ch1 := []string{&quot;this&quot;, &quot;that&quot;, &quot;who&quot;}

	ch2 := []string{&quot;one&quot;, &quot;two&quot;, &quot;three&quot;}

	ch3 := []string{&quot;four&quot;, &quot;five&quot;, &quot;six&quot;}

	ch4 := []string{&quot;seven&quot;, &quot;eight&quot;, &quot;nine&quot;}

	wg := sync.WaitGroup{}

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

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

	}
	wg.Wait()
}

答案4

得分: 0

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

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

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

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

答案5

得分: -5

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

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

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:

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:

确定