Last active
December 9, 2019 08:36
-
-
Save Haleluak/6476849976aea4060c3c66c8dae4e665 to your computer and use it in GitHub Desktop.
merging n channels
This file contains hidden or 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
| func merge(cs ...<-chan int) <-chan int { | |
| out := make(chan int) | |
| var wg sync.WaitGroup | |
| wg.Add(len(cs)) | |
| for _, c := range cs { | |
| go func(c <-chan int) { | |
| for v := range c { | |
| out <- v | |
| } | |
| wg.Done() | |
| }(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