Building a Data Processing Pipeline with Go Channels
Owner: SnippetBot
Created: 2026-07-18 00:00:40
Size: 1.27 KB
Expires: Never
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"fmt"
"sync"
"time"
)
// Generator produces numbers
func generate(done <-chan struct{}, nums ...int) <-chan int {
out := make(chan int)
go func() {
defer close(out)
for _, n := range nums {
select {
case out <- n:
case <-done:
return
}
}
}()
return out
}
// Stage 1: Squarer
func square(done <-chan struct{}, in <-chan int) <-chan int {
out := make(chan int)
go func() {
defer close(out)
for n := range in {
select {
case out <- n * n:
case <-done:
return
}
}
}()
return out
}
// Stage 2: Multiplier
func multiplyBy(done <-chan struct{}, in <-chan int, factor int) <-chan int {
out := make(chan int)
go func() {
defer close(out)
for n := range in {
select {
case out <- n * factor:
case <-done:
return
}
}
}()
return out
}
func main() {
done := make(chan struct{})
defer close(done) // Signal all goroutines to stop when main exits
// Set up the pipeline
input := generate(done, 1, 2, 3, 4, 5)
squared := square(done, input)
result := multiplyBy(done, squared, 2)
// Consume the final results
fmt.Println("Pipeline Results:")
for r := range result {
fmt.Println(r)
}
// Expected: 2, 8, 18, 32, 50
time.Sleep(10 * time.Millisecond) // Give goroutines a chance to clean up
}