-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovaQ1.go
59 lines (49 loc) · 819 Bytes
/
provaQ1.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
52
53
54
55
56
57
58
59
package main
import (
"fmt"
"time"
)
func handle(nServer int, itemCh chan int) chan Bid {
bidCh := make(chan Bid)
joinCh := make(chan int)
for i := 0; i < nServer; i++ {
go func() {
for item := range itemCh {
bidCh <- bid(item)
}
joinCh <- 1
}()
}
go func() {
for i := 0; i < nServer; i++ {
<-joinCh
}
close(joinCh)
close(bidCh)
}()
return bidCh
}
type Bid struct {
item int
bidValue int
bidFailed bool
}
func generateInput(inCh chan int) {
for i := 0; i < 15; i++ {
inCh <- i
}
close(inCh)
}
func bid(item int) Bid {
time.Sleep(time.Second * 5)
return Bid{item, item + 1, false}
}
func main() {
itemCh := make(chan int)
go generateInput(itemCh)
bidCh := handle(5, itemCh)
for bid := range bidCh {
fmt.Println("bid: ", bid)
}
fmt.Println("DONE!")
}