File tree 3 files changed +108
-1
lines changed
3 files changed +108
-1
lines changed Original file line number Diff line number Diff line change
1
+ package main
2
+
3
+ import (
4
+ "fmt"
5
+ "log"
6
+
7
+ "github.com/kevwan/mapreduce"
8
+ )
9
+
10
+ func main () {
11
+ val , err := mapreduce .MapReduce (func (source chan <- interface {}) {
12
+ for i := 0 ; i < 10 ; i ++ {
13
+ source <- i
14
+ }
15
+ }, func (item interface {}, writer mapreduce.Writer , cancel func (error )) {
16
+ i := item .(int )
17
+ writer .Write (i * i )
18
+ }, func (pipe <- chan interface {}, writer mapreduce.Writer , cancel func (error )) {
19
+ var sum int
20
+ for i := range pipe {
21
+ sum += i .(int )
22
+ }
23
+ writer .Write (sum )
24
+ })
25
+ if err != nil {
26
+ log .Fatal (err )
27
+ }
28
+ fmt .Println ("result:" , val )
29
+ }
Original file line number Diff line number Diff line change 53
53
54
54
很简单,goroutine 中监听一个全局的结束 channel 就行。
55
55
56
+ ## 简单示例
57
+
58
+ 并行求平方和(不要嫌弃示例简单,只是模拟并发)
59
+
60
+ ``` go
61
+ package main
62
+
63
+ import (
64
+ " fmt"
65
+ " log"
66
+
67
+ " github.com/kevwan/mapreduce"
68
+ )
69
+
70
+ func main () {
71
+ val , err := mapreduce.MapReduce (func (source chan <- interface {}) {
72
+ // generator
73
+ for i := 0 ; i < 10 ; i++ {
74
+ source <- i
75
+ }
76
+ }, func (item interface {}, writer mapreduce.Writer , cancel func (error )) {
77
+ // mapper
78
+ i := item.(int )
79
+ writer.Write (i * i)
80
+ }, func (pipe <- chan interface {}, writer mapreduce.Writer , cancel func (error )) {
81
+ // reducer
82
+ var sum int
83
+ for i := range pipe {
84
+ sum += i.(int )
85
+ }
86
+ writer.Write (sum)
87
+ })
88
+ if err != nil {
89
+ log.Fatal (err)
90
+ }
91
+ fmt.Println (" result:" , val)
92
+ }
93
+ ```
94
+
56
95
## 强烈推荐!
57
96
58
97
go-zero: [ https://github.com/zeromicro/go-zero ] ( https://github.com/zeromicro/go-zero )
59
98
60
99
## 欢迎 star!⭐
61
100
62
- 如果你正在使用或者觉得这个项目对你有帮助,请 ** star** 支持,感谢!
101
+ 如果你正在使用或者觉得这个项目对你有帮助,请 ** star** 支持,感谢!
Original file line number Diff line number Diff line change @@ -39,6 +39,45 @@ How can I terminate the process at any time?
39
39
40
40
It's simple, just listen to a global end channel or the given context in the goroutine.
41
41
42
+ ## A simple example
43
+
44
+ Calculate the sum of squares, simulating the concurrency.
45
+
46
+ ``` go
47
+ package main
48
+
49
+ import (
50
+ " fmt"
51
+ " log"
52
+
53
+ " github.com/kevwan/mapreduce"
54
+ )
55
+
56
+ func main () {
57
+ val , err := mapreduce.MapReduce (func (source chan <- interface {}) {
58
+ // generator
59
+ for i := 0 ; i < 10 ; i++ {
60
+ source <- i
61
+ }
62
+ }, func (item interface {}, writer mapreduce.Writer , cancel func (error )) {
63
+ // mapper
64
+ i := item.(int )
65
+ writer.Write (i * i)
66
+ }, func (pipe <- chan interface {}, writer mapreduce.Writer , cancel func (error )) {
67
+ // reducer
68
+ var sum int
69
+ for i := range pipe {
70
+ sum += i.(int )
71
+ }
72
+ writer.Write (sum)
73
+ })
74
+ if err != nil {
75
+ log.Fatal (err)
76
+ }
77
+ fmt.Println (" result:" , val)
78
+ }
79
+ ```
80
+
42
81
## References
43
82
44
83
go-zero: [ https://github.com/zeromicro/go-zero ] ( https://github.com/zeromicro/go-zero )
You can’t perform that action at this time.
0 commit comments