Skip to content

Instantly share code, notes, and snippets.

@campoy
Created January 15, 2018 05:16
Show Gist options
  • Save campoy/28014178f1de6c65acfc3806d25fc728 to your computer and use it in GitHub Desktop.
Save campoy/28014178f1de6c65acfc3806d25fc728 to your computer and use it in GitHub Desktop.
func merge(a, b <-chan int) <-chan int {
c := make(chan int)
go func() {
defer close(c)
for a != nil || b != nil {
select {
case v, ok := <-a:
if !ok {
fmt.Println("a is done")
a = nil
continue
}
c <- v
case v, ok := <-b:
if !ok {
fmt.Println("b is done")
b = nil
continue
}
c <- v
}
}
}()
return c
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment