1 Star 1 Fork 3

menuiis/gkit

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
fan_in.go 991 Bytes
一键复制 编辑 原始数据 按行查看 历史
SongZhibin97 提交于 2021-12-07 10:50 +08:00 . style: format code
package concurrent
import "reflect"
// FanInRec 扇入模式
func FanInRec(channels ...<-chan interface{}) <-chan interface{} {
out := make(chan interface{}, 1)
go func() {
defer close(out)
var cases []reflect.SelectCase
for _, channel := range channels {
cases = append(cases, reflect.SelectCase{
Dir: reflect.SelectRecv,
Chan: reflect.ValueOf(channel),
})
}
for len(cases) > 0 {
i, v, ok := reflect.Select(cases)
if !ok {
// 监控的channel已经关闭
cases = append(cases[:i], cases[i+1:]...)
continue
}
out <- v.Interface()
}
}()
return out
}
// MergeChannel 合并两个channel
func MergeChannel(a, b <-chan interface{}) <-chan interface{} {
c := make(chan interface{})
go func() {
defer close(c)
for a != nil || b != nil {
select {
case v, ok := <-a:
if !ok {
a = nil
continue
}
c <- v
case v, ok := <-b:
if !ok {
b = nil
continue
}
c <- v
}
}
}()
return c
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/menciis/gkit.git
git@gitee.com:menciis/gkit.git
menciis
gkit
gkit
d3f65ed26d21

搜索帮助