Created
August 18, 2019 08:54
-
-
Save Tomotoes/3b1695dae80d5fa5708a78eab34d605b to your computer and use it in GitHub Desktop.
Merge Channels
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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