内容简介:bridges/bridges.gobridge_demo.go程序输出如下,
bridges/bridges.go
// bridges package bridges type BridgeO struct { } func NewBridge() *BridgeO { bridgeO := &BridgeO{} return bridgeO } func (bridgeO *BridgeO) OrDone( done, c <-chan interface{}, ) <-chan interface{} { valStream := make(chan interface{}) go func() { defer close(valStream) for { select { case <-done: return case v, ok := <-c: if ok == false { return } select { case valStream <- v: case <-done: } } } }() return valStream } func (bridgeO *BridgeO) Bridge( done <-chan interface{}, chanStream <-chan <-chan interface{}, ) <-chan interface{} { valStream := make(chan interface{}) go func() { defer close(valStream) for { var stream <-chan interface{} select { case maybeStream, ok := <-chanStream: if ok == false { return } stream = maybeStream case <-done: return } for val := range bridgeO.OrDone(done, stream) { select { case valStream <- val: case <-done: } } } }() return valStream }
bridge_demo.go
// bridgedemo package main import ( "bridgedemo/bridges" "fmt" ) func main() { bridge := bridges.NewBridge() genVals := func() <-chan <-chan interface{} { chanStream := make(chan (<-chan interface{})) go func() { defer close(chanStream) for i := 0; i < 10; i++ { stream := make(chan interface{}, 1) stream <- i close(stream) chanStream <- stream } }() return chanStream } done := make(chan interface{}) defer close(done) for v := range bridge.Bridge(done, genVals()) { fmt.Printf("%v ", v) } fmt.Println() }
程序输出如下,
image.png
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。