使用Golang创建带有容器/通道环的负载均衡器

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

creating load balancer with container/ring of channels in golang

问题

我正在尝试使用容器/通道环来创建负载均衡器,但在写入它们时遇到问题。环似乎将interface {}作为类型,这在我尝试写入其分配的通道时会导致问题。

出现的错误是:

  1. prog.go:11: invalid operation: chring.Value <- true (send to non-chan type interface {})

简化的代码:http://play.golang.org/p/AJs2MV_UUC

  1. package main
  2. //import "fmt"
  3. import "container/ring"
  4. func main() {
  5. chring := ring.New(10)
  6. for i:=0;i<10;i++ {
  7. ch:=make(chan bool)
  8. chring.Value=ch
  9. chring.Value <- true //dies here
  10. chring = chring.Next()
  11. }
  12. }
英文:

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

  1. prog.go:11: invalid operation: chring.Value &lt;- true (send to non-chan type interface {})

simplified code: http://play.golang.org/p/AJs2MV_UUC

  1. package main
  2. //import &quot;fmt&quot;
  3. import &quot;container/ring&quot;
  4. func main() {
  5. chring := ring.New(10)
  6. for i:=0;i&lt;10;i++ {
  7. ch:=make(chan bool)
  8. chring.Value=ch
  9. chring.Value &lt;- true //dies here
  10. chring = chring.Next()
  11. }
  12. }

答案1

得分: 3

使用类型断言:

  1. chring.Value.(chan bool) <- true
英文:

Use a type assertion:

  1. chring.Value.(chan bool) &lt;- true

huangapple
  • 本文由 发表于 2013年5月15日 23:53:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/16569703.html
匿名

发表评论

匿名网友

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

确定