From 0a42c8cf0a87ad3dd2bbc2e38b7fc7880f7443fa Mon Sep 17 00:00:00 2001 From: Timon Ringwald Date: Sun, 28 Aug 2022 15:28:33 +0200 Subject: [PATCH] fixed bugs in TeeMany --- utils.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/utils.go b/utils.go index bea7182..8dda8cd 100644 --- a/utils.go +++ b/utils.go @@ -39,21 +39,28 @@ func Tee[T any](source <-chan T) (<-chan T, <-chan T) { // TeeMany returns a given amount of channels which all receive all values from source. // It's basically a copy function for channels func TeeMany[T any](source <-chan T, amount int) []<-chan T { - outs := make([]chan T, amount) + outputs := make([]chan T, amount) + for i := range outputs { + outputs[i] = make(chan T, cap(source)) + } go func() { defer func() { - for _, out := range outs { + for _, out := range outputs { close(out) } }() for value := range source { - for _, out := range outs { + for _, out := range outputs { out <- value } } }() - return (interface{}(outs)).([]<-chan T) + readOnlyOutputs := make([]<-chan T, 0, amount) + for _, out := range outputs { + readOnlyOutputs = append(readOnlyOutputs, out) + } + return readOnlyOutputs }