Created
July 24, 2018 04:06
-
-
Save hauxe/7a49e3f38fa522fa61a19d8548a1997c to your computer and use it in GitHub Desktop.
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
// OR function combines all signal from channels into a single channel with Or condition | |
func OR(channels ...<-chan interface{}) <-chan interface{} { | |
switch len(channels) { | |
case 0: | |
return nil | |
case 1: | |
return channels[0] | |
} | |
orDone := make(chan interface{}) | |
go func() { | |
defer close(orDone) | |
switch len(channels) { | |
case 2: | |
select { | |
case <-channels[0]: | |
case <-channels[1]: | |
} | |
default: | |
select { | |
case <-channels[0]: | |
case <-channels[1]: | |
case <-channels[2]: | |
case <-OR(append(channels[3:], orDone)...): | |
} | |
} | |
}() | |
return orDone | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment