Created
January 25, 2020 09:47
-
-
Save tsonglew/efc6622b2685c4f8f9d1feb79470e231 to your computer and use it in GitHub Desktop.
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
package main | |
import "fmt" | |
func orChan(chans ...chan interface{}) chan interface{} { | |
switch len(chans) { | |
case 0: | |
return nil | |
case 1: | |
return chans[0] | |
} | |
done := make(chan interface{}) | |
go func() { | |
defer close(done) | |
select { | |
case <-chans[0]: | |
case <-orChan(chans[1:]...): | |
} | |
}() | |
return done | |
} | |
func main() { | |
chans := make([]chan interface{}, 6) | |
for i := range chans { | |
chans[i] = make(chan interface{}) | |
} | |
close(chans[5]) | |
fmt.Println(<-orChan(chans...)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment