英文:
Is there an upper limit when using select in Go?
问题
在Go语言中使用select语句时,你可以监听多少个case呢?是否有上限,比如说10k?过多的case会带来不良影响吗?
英文:
When I use select in Go, how many cases could I listen? Is there an upper limit on it, for example, 10k? Will overmuch cases bring harmful effects?
答案1
得分: 2
不,没有实际的上限。
英文:
No, there is no practical upper limit.
答案2
得分: 1
我不相信有这样的方法。然而,select
语句必须在编译时显式编写,所以除非你打算自动生成该select
语句的代码,否则编写起来可能会很痛苦。如果你有一个需要在多个通道上进行选择的大型通道列表,你可以尝试使用以下代码:
for {
for _, c := range channels {
select {
case val := <-c:
// 代码...
default:
// 代码...
}
}
}
英文:
I don't believe that there is. However, select
s must be written explicitly at compile-time, so unless you plan on auto-generating the code for that select statement, that sounds painful to write. If you have a big list of channels you need to select over, you should try this instead:
for {
for _, c := range channels {
select {
case val := <-c:
// code...
default:
// code...
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论