Skip to content

Instantly share code, notes, and snippets.

@dannypsnl
Last active February 13, 2018 09:41
Show Gist options
  • Save dannypsnl/a3d8262cfb9b1dbfb94f897eea24aff7 to your computer and use it in GitHub Desktop.
Save dannypsnl/a3d8262cfb9b1dbfb94f897eea24aff7 to your computer and use it in GitHub Desktop.
var Or func(channels ...<-chan interface{}) <-chan interface{}
func init() {
Or = func(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
}
}
sig := func(after time.Duration) <-chan interface{} {
c := make(chan interface{})
go func() {
defer close(c)
time.Sleep(after)
}()
return c
}
start := time.Now()
<-Or(
sig(10*time.Second),
sig(5*time.Second),
sig(1*time.Second),
)
tm := time.Since(start)
if tm > 2*time.Second || tm < time.Second {
t.Error(`error`)
}
@dannypsnl
Copy link
Author

race fn Or

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment