-
Notifications
You must be signed in to change notification settings - Fork 1
/
program.go
66 lines (54 loc) · 1.47 KB
/
program.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
60
61
62
63
64
65
66
package main
import (
"fmt"
)
//Consumer
type Consumer struct {
items *chan int
timestamp *chan string
}
//creating a new Consumer
func NewConsumer(items *chan int) *Consumer {
return &Consumer{items: items, timestamp: timestamp}
}
//consuming items from the channel
func (c *Consumer) consume() {
fmt.Println("consume: Started")
for {
item := <-*c.items
timestamps := <-*c.timestamp
fmt.Println("Received:", item, " which was produced at: ",timestamps)
}
}
//Producer
type Producer struct {
items *chan int
done *chan bool
timestamp *chan string
}
//creating a new Producer
func NewProducer(items *chan int, done *chan bool, timestamp *chan string) *Producer {
return &Producer{items: items, done: done, timestamp: timestamp}
}
//producing and sending the item through the channel
func (p *Producer) produce(max int) {
fmt.Println("produce: Started")
for i := 0; i < max; i++ {
fmt.Println("produce: Sending ", i)
*p.items <- i
*p.timestamp <- string(time.crrentTime)
}
*p.done <- true // signal when done
fmt.Println("produce: Done")
}
func main() {
var items = make(chan int) // channel to send items
var done = make(chan bool) // channel to control when production is done
var timestamp = make(chan string)
// Start a goroutine for Produce.produce
go NewProducer(&items, &done, ×tamp).produce(5)
// Start a goroutine for Consumer.consume
go NewConsumer(&items, ×tamp).consume()
// Finish the program when the production is done
<-done
}