Skip to content

Instantly share code, notes, and snippets.

@AGMETEOR
Created January 4, 2021 18:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AGMETEOR/47a6de16ee57854648562b5163dc464e to your computer and use it in GitHub Desktop.
Save AGMETEOR/47a6de16ee57854648562b5163dc464e to your computer and use it in GitHub Desktop.
Go channel generator functions with multiplexing
// support for arbitrary number of channels of type string
func nonDeterministicChanMultiPlexer(channels ...chan string) <-chan string {
outChan := make(chan string)
for _, c := range channels {
go func(cc chan string) {
for {
outChan <- <-cc
}
}(c)
}
return outChan
}
// support for known number of channels, in this case just 2
func chanMultiPlexer(c1, c2 chan string) <-chan string {
outChan := make(chan string)
go func() {
for {
select {
case s := <-c1:
outChan <- s
case s := <-c2:
outChan <- s
}
}
}()
return outChan
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment