-
Notifications
You must be signed in to change notification settings - Fork 0
/
goroutines_practice.go
executable file
·55 lines (46 loc) · 1.09 KB
/
goroutines_practice.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
package main
import (
"fmt"
"time"
"strconv"
"sync"
)
func SetsWaitVariables(channel chan string, waitVar sync.WaitGroup){
defer waitVar.Done()
SetsVariables(channel)
}
func GetsWaitVariables(channel chan string, waitVar sync.WaitGroup){
defer waitVar.Done()
GetsVariables(channel)
// Insted of using defer, waitVar.Done() can also be called at the end of function
}
func GetsVariables(channel chan string){
for str := range channel{
fmt.Println(str)
}
}
func SetsVariables(channel chan string){
for i:=0; i<10; i++{
channel <- ("string_"+strconv.Itoa(i))
time.Sleep(time.Second)
}
close(channel)
}
func WaitBasedGoRoutines(channel chan string){
var waitVar sync.WaitGroup
waitVar.Add(2)
go SetsWaitVariables(channel, waitVar)
go GetsWaitVariables(channel, waitVar)
waitVar.Wait()
}
func NoobGoRoutines(channel chan string){
go SetsVariables(channel)
go GetsVariables(channel)
var input string
fmt.Scanln(&input)
}
func main(){
channel := make(chan string)
WaitBasedGoRoutines(channel)
// NoobGoRoutines(channel)
}