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

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

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 &lt;- true (send to non-chan type interface {})

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

package main

//import &quot;fmt&quot;
import &quot;container/ring&quot;

func main() {
	chring := ring.New(10)
	for i:=0;i&lt;10;i++ {
		ch:=make(chan bool)
		chring.Value=ch
		chring.Value &lt;- true //dies here
		chring = chring.Next()
	}	
	

}

答案1

得分: 3

使用类型断言:

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

Use a type assertion:

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:

确定