Skip to content

Instantly share code, notes, and snippets.

@Tomotoes
Created August 18, 2019 08:54
Show Gist options
  • Save Tomotoes/3b1695dae80d5fa5708a78eab34d605b to your computer and use it in GitHub Desktop.
Save Tomotoes/3b1695dae80d5fa5708a78eab34d605b to your computer and use it in GitHub Desktop.
Merge Channels
package main
import "sync"
func merge(cs ...chan interface{}) <-chan interface{} {
var wg sync.WaitGroup
out := make(chan interface{})
output := func(c <-chan interface{}) {
for e := range c {
out <- e
}
wg.Done()
}
wg.Add(len(cs))
for _, c := range cs {
go output(c)
}
go func() {
wg.Wait()
close(out)
}()
return out
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment