英文:
creating load balancer with container/ring of channels in golang
问题
我正在尝试使用容器/通道环来创建负载均衡器,但在写入它们时遇到问题。环似乎将interface {}作为类型,这在我尝试写入其分配的通道时会导致问题。
出现的错误是:
prog.go:11: invalid operation: chring.Value <- true (send to non-chan type interface {})
简化的代码:http://play.golang.org/p/AJs2MV_UUC
package main
//import "fmt"
import "container/ring"
func main() {
chring := ring.New(10)
for i:=0;i<10;i++ {
ch:=make(chan bool)
chring.Value=ch
chring.Value <- true //dies here
chring = chring.Next()
}
}
英文:
I am trying to make load balancer with container/ring of channels and I am having problems writing to them. Ring seems to take interface {} as type which causes problem when I try to write to it's assigned channel.
Error that comes out is
prog.go:11: invalid operation: chring.Value <- true (send to non-chan type interface {})
simplified code: http://play.golang.org/p/AJs2MV_UUC
package main
//import "fmt"
import "container/ring"
func main() {
chring := ring.New(10)
for i:=0;i<10;i++ {
ch:=make(chan bool)
chring.Value=ch
chring.Value <- true //dies here
chring = chring.Next()
}
}
答案1
得分: 3
使用类型断言:
chring.Value.(chan bool) <- true
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论