Skip to content

Instantly share code, notes, and snippets.

@hauxe
Created July 24, 2018 04:15
Show Gist options
  • Save hauxe/86429a93510cba216c9d023cee54aaa8 to your computer and use it in GitHub Desktop.
Save hauxe/86429a93510cba216c9d023cee54aaa8 to your computer and use it in GitHub Desktop.
// AND function combines all signal from channels into a single channel with And condition
func AND(channels ...<-chan interface{}) <-chan interface{} {
switch len(channels) {
case 0:
return nil
case 1:
return channels[0]
}
andDone := make(chan interface{})
collector := make(chan interface{}, len(channels))
go func() {
defer close(andDone)
switch len(channels) {
case 2:
select {
case collector <- <-channels[0]:
case collector <- <-channels[1]:
}
default:
select {
case collector <- <-channels[0]:
case collector <- <-channels[1]:
case collector <- <-channels[2]:
case collector <- <-AND(channels[3:]...):
}
}
}()
return andDone
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment