-
-
Save yookoala/57a2b52b8a848814329631f2c7492bdd 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
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