-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
68 lines (61 loc) · 1.42 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
package main
import (
"async/api"
"async/mocks"
"fmt"
"time"
)
func callSimulatorAsync(done chan string) {
go mocks.AsyncA(done)
go mocks.AsyncB(done)
}
func callSimulatorSync() {
fmt.Println(mocks.SyncA())
fmt.Println(mocks.SyncB())
}
func callQuoteSync() {
for i := 0 ;i < 15; i++ {
quote := api.GetQuote()
fmt.Println(quote)
}
}
func callQuoteAsync(done chan string) {
for i := 0 ;i < 15; i++ {
go api.GetQuoteAsync(done)
}
for i := 0 ;i < 15; i++ {
fmt.Println(<-done)
}
}
func getEpisodes() {
episodes := api.GetEpisodes()
names := make([]chan string, len(episodes))
for i := 0; i < len(episodes); i++ {
names[i] = make(chan string)
go api.GetEpisodeName(episodes[i], names[i])
}
for i := 0; i < len(episodes); i++ {
fmt.Println(<-names[i])
}
}
func main() {
fmt.Println("Getting episodes for Rick&Morty")
getEpisodes()
fmt.Println("Start simulator sync", time.Now())
callSimulatorSync()
fmt.Println("End simulator sync", time.Now())
fmt.Println("Start simulator async", time.Now())
done := make(chan string)
callSimulatorAsync(done)
fmt.Println("Start simulator async", time.Now())
fmt.Println(<-done)
fmt.Println(<-done)
fmt.Println("Start quote sync", time.Now())
callQuoteSync()
fmt.Println("End quote sync", time.Now())
fmt.Println("Start quote async", time.Now())
fmt.Println(time.Now())
callQuoteAsync(done)
fmt.Println(time.Now())
fmt.Println("End quote async", time.Now())
}