add missing merge.go
This commit is contained in:
parent
3a48df7622
commit
8f02aff20e
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue