Skip to content

Instantly share code, notes, and snippets.

@yookoala
Forked from campoy/main.go
Last active April 19, 2019 08:36
Show Gist options
  • Save yookoala/57a2b52b8a848814329631f2c7492bdd to your computer and use it in GitHub Desktop.
Save yookoala/57a2b52b8a848814329631f2c7492bdd to your computer and use it in GitHub Desktop.
func merge(cs ...<-chan int) <-chan int {
out := make(chan int)
var cases []reflect.SelectCase
for _, c := range chans {
cases = append(cases, reflect.SelectCase{
Dir: reflect.SelectRecv,
Chan: reflect.ValueOf(c),
})
}
go func() {
for {
i, v, ok := reflect.Select(cases)
if !ok {
if len(cases) <= 1 {
break
}
cases = append(cases[:i], cases[i+1:]...)
continue
}
out <- v.Interface().(*objectState)
}
close(out)
}()
return out
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment