Skip to content

Instantly share code, notes, and snippets.

@oNddleo
Created June 26, 2023 09:33
Show Gist options
  • Save oNddleo/36f5a67f1ca79535373d497efa4a8d96 to your computer and use it in GitHub Desktop.
Save oNddleo/36f5a67f1ca79535373d497efa4a8d96 to your computer and use it in GitHub Desktop.
Merged Channel Example
package main
import (
"fmt"
"sync"
)
func streamNumbers(numbers ...int) <-chan int {
c := make(chan int)
go func() {
for n := range numbers {
c <- n
}
close(c)
}()
return c
}
func sumAllStreams(streams ...<-chan int) <-chan int {
sumChan := make(chan int)
counter := 0
wc := new(sync.WaitGroup)
wc.Add(len(streams))
for i := 0; i < len(streams); i++ {
go func(s <-chan int) {
for n := range s {
counter += n
}
wc.Done()
}(streams[i])
}
go func() {
wc.Wait()
sumChan <- counter
}()
return sumChan
}
func main() {
s := sumAllStreams(
streamNumbers(1, 2, 3, 4, 5),
streamNumbers(8, 8, 3, 3, 10, 12, 14),
streamNumbers(1, 1, 2, 2, 4, 4, 6),
)
fmt.Println(<-s)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment