在golang中使用容器/通道环创建负载均衡器

时间:2013-05-15 15:53:47

标签: go

我正在尝试使用容器/通道环来制作负载均衡器,我在写入它们时遇到了问题。 Ring似乎将接口{}作为类型,当我尝试写入其指定的通道时会导致问题。

出现的错误是

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


}

1 个答案:

答案 0 :(得分:3)

使用type assertion

chring.Value.(chan bool) <- true