-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipeline.go
51 lines (40 loc) · 789 Bytes
/
pipeline.go
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
package main
import (
"fmt"
"sort"
)
func PipelineExample() {
fmt.Println("--------------------------")
nums := []int{3, 5, 2, 6, 8, 0, 2, 1}
stage1 := make(chan []int)
go sortStage(nums, stage1)
stage2 := make(chan int)
go sliceStage(stage1, stage2)
stage3 := make(chan int)
go plusStage(stage2, stage3)
<-stage3
fmt.Println("Hello From Pipeline!")
}
func sortStage(nums []int, out chan<- []int) {
sort.Slice(nums, func(i, j int) bool {
return nums[j] < nums[i]
})
out <- nums
close(out)
}
func sliceStage(in <-chan []int, out chan<- int) {
nums := <-in
for _, v := range nums {
out <- v
}
close(out)
}
func plusStage(in <-chan int, out chan<- int) {
sum := 0
for v := range in {
sum += v
}
fmt.Println("Received:", sum)
out <- sum
close(out)
}