-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
148 lines (119 loc) · 2.73 KB
/
main.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"fmt"
"sync"
"time"
)
func hello(channel chan bool) {
fmt.Println("Hello world goroutine")
channel <- true //writing to the channel
}
func calcSquare(num int, square chan int) {
sum := 0
for num != 0 {
digit := num % 10
sum += digit * digit
num /= 10
}
square <- sum //writing sum value to the channel
}
func calcCubes(num int, cube chan int) {
sum := 0
for num != 0 {
digit := num % 10
sum += digit * digit * digit
num /= 10
}
cube <- sum
}
func sendData(channel chan <- int) {
channel <- 19
}
func printNum(channel chan int) {
for i := 0; i < 10; i++ {
channel <- i
}
close(channel)
}
func writeNum(channel chan int) {
for i := 0; i < 5; i++ {
channel <- i
fmt.Println("Wrote ", i, " to channel")
}
close(channel)
}
//pointer should be passed
//else each goroutine will have their own copy of waitGroup
//bc functions are pass by value
func process(i int, waitGroup *sync.WaitGroup) {
fmt.Println("Started goroutine ", i)
time.Sleep(2 * time.Second)
fmt.Println("Ended goroutine ", i)
waitGroup.Done() //decrement counter
}
func main() {
done := make(chan bool) //creating a channel
go hello(done)
//reading from the channel
//data received is not stored - legal
//main go routine is blocked until data is received from the channel
<- done
//time.Sleep(1 * time.Second)
fmt.Println("main function - after hello()")
//2nd example
number := 678
square := make(chan int)
cube := make(chan int)
go calcSquare(number, square)
go calcCubes(number, cube)
//reading from channels
squares, cubes := <-square, <-cube
fmt.Println("Final output ", squares + cubes)
//example 3
channel := make(chan int)
go sendData(channel)
fmt.Println(<-channel)
//example 4
ch := make(chan int)
go printNum(ch)
for {
v, ok := <- ch
if ok == false {
break
}
fmt.Println("received ", v, ok)
}
fmt.Println()
//example 5
ch2 := make(chan int)
go printNum(ch2)
for v := range ch2 {
fmt.Println("Received ", v)
}
fmt.Println()
//example 6 - buffered channels
channel2 := make(chan string, 2)
channel2 <- "Shenali"
channel2 <- "Jayakody"
fmt.Println(<-channel2)
fmt.Println(<-channel2)
fmt.Println()
//example 7
channel3 := make(chan int, 2)
go writeNum(channel3)
time.Sleep(2 * time.Second)
for v := range channel3 {
fmt.Println("Read ", v, " from channel3")
time.Sleep(2 * time.Second)
}
fmt.Println()
//waitGroup
no := 3
var wGroup sync.WaitGroup //zero value waitGroup is created.
for i := 0; i < no; i++ {
wGroup.Add(1) //increment counter of waitGroup by one
go process(i, &wGroup) //goroutine
}
wGroup.Wait() //blocks the main goroutine until the counter of the waitGroup becomes zero
fmt.Println("All go routines finished executing")
}