Created
January 4, 2021 18:13
-
-
Save AGMETEOR/47a6de16ee57854648562b5163dc464e to your computer and use it in GitHub Desktop.
Go channel generator functions with multiplexing
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
// 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