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 }