add missing merge.go

This commit is contained in:
Adam Babik 2018-11-29 13:04:43 +01:00
parent 3a48df7622
commit 8f02aff20e
No known key found for this signature in database
GPG Key ID: ED02515A1FC0D1B4
1 changed files with 28 additions and 0 deletions

View File

@ -0,0 +1,28 @@
package main
import (
"sync"
)
// T is a type which is handled by merge function.
// Type alias is used to allow easy type change.
type T = error
func merge(cs ...<-chan T) <-chan T {
out := make(chan T)
var wg sync.WaitGroup
wg.Add(len(cs))
for _, c := range cs {
go func(c <-chan T) {
for v := range c {
out <- v
}
wg.Done()
}(c)
}
go func() {
wg.Wait()
close(out)
}()
return out
}