Golang 使用tee将一个channel分拆成两个相同的channel

栏目: Go · 发布时间: 4年前

内容简介:tees/channels.goteedemo.go程序输出如下

tees/channels.go

package tees

type TeeOp struct {
}

func NewTeeOp() *TeeOp {
    teeOp := &TeeOp{}
    return teeOp
}

func (teeOp *TeeOp) 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 (teeOp *TeeOp) Tee(
    done <-chan interface{},
    in <-chan interface{},
) (_, _ <-chan interface{}) {
    out1 := make(chan interface{})
    out2 := make(chan interface{})
    go func() {
        defer close(out1)
        defer close(out2)
        for val := range teeOp.OrDone(done, in) {
            var out1, out2 = out1, out2
            for i := 0; i < 2; i++ {
                select {
                case <-done:
                case out1 <- val:
                    out1 = nil
                case out2 <- val:
                    out2 = nil
                }
            }
        }
    }()
    return out1, out2
}

func (teeOp *TeeOp) Repeat(
    done <-chan interface{},
    args ...interface{},
) <-chan interface{} {
    valueStream := make(chan interface{})
    go func() {
        defer close(valueStream)
        for {
            for _, v := range args {
                select {
                case <-done:
                    return
                case valueStream <- v:
                }
            }
        }
    }()
    return valueStream
}

func (teeOp *TeeOp) Take(
    done <-chan interface{},
    valueStream <-chan interface{},
    num int,
) <-chan interface{} {
    takeStream := make(chan interface{})

    go func() {
        defer close(takeStream)
        for i := 0; i < num; i++ {
            select {
            case <-done:
                return
            case takeStream <- <-valueStream:
            }
        }
    }()
    return takeStream
}

teedemo.go

package main

import (
    "fmt"
    "teedemo/tees"
)

func main() {
    teeOp := tees.NewTeeOp()
    done := make(chan interface{})
    defer close(done)

    out1, out2 := teeOp.Tee(done, teeOp.Take(done, teeOp.Repeat(done, 1, 2, 3), 6))
    for val1 := range out1 {
        fmt.Printf("out1: %v, out2: %v \n", val1, <-out2)
    }
}

程序输出如下

Golang 使用tee将一个channel分拆成两个相同的channel

image.png


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Landing Page Optimization

Landing Page Optimization

Tim Ash / Wiley Publishing / 2008-1-29 / USD 29.99

在线阅读本书 How much money are you losing because of poor landing page design? In this comprehensive, step-by-step guide, you’ll learn all the skills necessary to dramatically improve your bottom li......一起来看看 《Landing Page Optimization》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

html转js在线工具
html转js在线工具

html转js在线工具