snippetgoModeratepending
Go channel patterns -- fan-out, fan-in, and pipeline
Viewed 0 times
channelfan-outfan-inpipelinegoroutineselect
go
Problem
Need to parallelize work across multiple goroutines and collect results. Or process data through a series of stages concurrently.
Solution
Use Go channels for communication between goroutines. Common patterns: fan-out (one-to-many), fan-in (many-to-one), pipeline (staged processing).
Code Snippets
Channel patterns: fan-out, fan-in, pipeline
// Fan-out: distribute work to N workers
func fanOut(input <-chan int, workers int) []<-chan int {
channels := make([]<-chan int, workers)
for i := 0; i < workers; i++ {
channels[i] = worker(input)
}
return channels
}
// Fan-in: merge N channels into one
func fanIn(channels ...<-chan int) <-chan int {
out := make(chan int)
var wg sync.WaitGroup
for _, ch := range channels {
wg.Add(1)
go func(c <-chan int) {
defer wg.Done()
for v := range c { out <- v }
}(ch)
}
go func() { wg.Wait(); close(out) }()
return out
}
// Pipeline: stage1 -> stage2 -> stage3
func pipeline(nums <-chan int) <-chan string {
doubled := stage(nums, func(n int) int { return n * 2 })
formatted := stageStr(doubled, func(n int) string {
return fmt.Sprintf("result: %d", n)
})
return formatted
}Revisions (0)
No revisions yet.