package main

import (
    "context"
    "fmt"
    "time"
)

func teeChannel(ctx context.Context, value <-chan int) (<-chan int, <-chan int) {
    ch1 := make(chan int)
    ch2 := make(chan int)

    go func() {
        defer close(ch1)
        defer close(ch2)
        for v := range value {
            var ch1, ch2 = ch1, ch2
            for i := 0; i < 2; i++ {
                select {
                case <-ctx.Done():
                    return
                case ch1 <- v:
                    ch1 = nil //避免重复发送,置空
                case ch2 <- v:
                    ch2 = nil
                }
            }
        }
    }()

    return ch1, ch2
}

func main() {
    ctx, cancel := context.WithCancel(context.Background())
    defer cancel()

    stream := make(chan int)

    ch1, ch2 := teeChannel(ctx, stream)

    go func() {
        defer close(stream)
        for i := 0; i < 5; i++ {
            stream <- i + 1
            time.Sleep(time.Millisecond * 100)
        }
    }()

    for {
        select {
        case <-ctx.Done():
            return
        case v, ok := <-ch1:
            if !ok {
                return
            }
            fmt.Printf("ch1:%d \n", v)
        case v, ok := <-ch2:
            if !ok {
                return
            }
            fmt.Printf("ch2:%d \n", v)
        }
        if ch1 == nil && ch2 == nil {
            break
        }
    }
    fmt.Println("All Done")
}

 【版权申明】未经博主同意,谢绝转载!(请尊重原创,博主保留追究权) https://www.cnblogs.com/facetwitter/p/18293868

posted @ 2024-07-10 13:13 saneim 阅读(7) 评论(0) 推荐(0) 编辑
摘要: package main import ( "context" "fmt" ) // orDone func orDone(ctx context.Context, value <-chan int) <-chan int { ordoneStream := make(chan int) go fu 阅读全文
posted @ 2024-07-10 13:11 saneim 阅读(41) 评论(0) 推荐(0) 编辑
摘要: 扇入扇出寻找素数: package main import ( "fmt" "math/rand" "runtime" "sync" "time" ) var repeatFn = func(done <-chan interface{}, fn func() interface{}) <-chan 阅读全文
posted @ 2024-07-10 13:09 saneim 阅读(40) 评论(0) 推荐(0) 编辑
摘要: package main import ( "fmt" "net/http" ) type Results struct { Error error Response *http.Response } func main() { checkStatus := func(done <-chan int 阅读全文
posted @ 2024-07-10 13:06 saneim 阅读(5) 评论(0) 推荐(0) 编辑
摘要: package main import ( "fmt" "math/rand" ) func main() { pFn := func(done <-chan interface{}, fn func() int) <-chan int { valueStream := make(chan int) 阅读全文
posted @ 2024-07-10 13:04 saneim 阅读(3) 评论(0) 推荐(0) 编辑
摘要: package main import ( "fmt" "time" ) func main() { var or func(channels ...<-chan interface{}) <-chan interface{} or = func(channels ...<-chan interfa 阅读全文
posted @ 2024-07-10 13:01 saneim 阅读(6) 评论(0) 推荐(0) 编辑
摘要: 使用使用LocalDate打印日历java代码: import java.time.*; public class getDate { public static void main(String[] args) { System.out.println("Mon Tue Wed Thu Fir S 阅读全文
posted @ 2023-10-25 22:10 saneim 阅读(22) 评论(0) 推荐(0) 编辑
摘要: 在与三方公司对接时,三方公司调用我们的接口发生频繁的连接超时问题,并发量不大。服务器系统版本为centos7.9,网上查了一些资料,找到解决办法: 1.查看tcp_tw_reuse值,默认为0:cat /proc/sys/net/ipv4/tcp_tw_reuse 2.修改tcp_tw_reuse值 阅读全文
posted @ 2023-03-22 11:43 saneim 阅读(89) 评论(0) 推荐(0) 编辑
摘要: docker容器启动不起来,并报错:Error response from daemon: OCI runtime create failed: container with id exists: 211aaa83e999f76993e48d7fa1bc6c3c9cbb2f54c396a248545 阅读全文
posted @ 2023-03-20 14:39 saneim 阅读(1048) 评论(0) 推荐(0) 编辑
摘要: 1.拉取redis镜像: docker pull redis 2.查看本地镜像: docker images 3.从github下载对应版本的redis.conf配置文件:打开链接 https://github.com/redis/redis/,找到redis.conf,下载文件。将redis.co 阅读全文
posted @ 2022-11-22 22:03 saneim 阅读(2910) 评论(0) 推荐(0) 编辑
点击右上角即可分享
微信分享提示