Created
April 3, 2022 08:57
-
-
Save stvoidit/213a7df93f38fd2376da721f5b397aa6 to your computer and use it in GitHub Desktop.
generics channels merge
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[T any, C chan T](cs ...C) C { | |
out := make(C) | |
var wg sync.WaitGroup | |
wg.Add(len(cs)) | |
for i := range cs { | |
go func(c C) { | |
defer wg.Done() | |
for value := range c { | |
out <- value | |
} | |
}(cs[i]) | |
} | |
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
usage example: