This repository was archived by the owner on Feb 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathpassingfields.go
More file actions
60 lines (49 loc) · 1.28 KB
/
passingfields.go
File metadata and controls
60 lines (49 loc) · 1.28 KB
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
// +build ignore
package main
import (
"context"
"fmt"
"github.com/catmullet/go-workers"
"math/rand"
)
func main() {
ctx := context.Background()
workerOne := workers.NewRunner(ctx, NewWorkerOne(2), 100).Start()
workerTwo := workers.NewRunner(ctx, NewWorkerTwo(4), 100).InFrom(workerOne).Start()
for i := 0; i < 15; i++ {
workerOne.Send(rand.Intn(100))
}
if err := workerOne.Wait(); err != nil {
fmt.Println(err)
}
if err := workerTwo.Wait(); err != nil {
fmt.Println(err)
}
}
type WorkerOne struct {
amountToMultiply int
}
type WorkerTwo struct {
amountToMultiply int
}
func NewWorkerOne(amountToMultiply int) workers.Worker {
return &WorkerOne{
amountToMultiply: amountToMultiply,
}
}
func NewWorkerTwo(amountToMultiply int) workers.Worker {
return &WorkerTwo{
amountToMultiply,
}
}
func (wo *WorkerOne) Work(in interface{}, out chan<- interface{}) error {
total := in.(int) * wo.amountToMultiply
fmt.Println("worker1", fmt.Sprintf("%d * %d = %d", in.(int), wo.amountToMultiply, total))
out <- total
return nil
}
func (wt *WorkerTwo) Work(in interface{}, out chan<- interface{}) error {
totalFromWorkerOne := in.(int)
fmt.Println("worker2", fmt.Sprintf("%d * %d = %d", totalFromWorkerOne, wt.amountToMultiply, totalFromWorkerOne*wt.amountToMultiply))
return nil
}