-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
84 lines (72 loc) · 1.84 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
package main
import (
"fmt"
"log"
"math/rand"
"sync"
"time"
)
type Girl struct {
hot bool
match bool
}
func main() {
start := time.Now()
var wg sync.WaitGroup // Create waitGroup to determine when all goRoutines are finished.
rand.Seed(time.Now().UnixNano()) // To be sure we have a random result every try.
//Init Mockdata
theGirls := make([]Girl, 0)
theGirls = InitGirls(3000)
//Channels
likedChannel := make(chan Girl, 3) // Buffered channel
matchedChannel := make(chan Girl)
//Start GoRoutines
wg.Add(3)
go SwipeAndLike(theGirls, likedChannel, &wg)
go FindMatches(likedChannel, matchedChannel, &wg)
go StartConversations(matchedChannel, &wg)
wg.Wait()
elapsed := time.Since(start)
log.Printf("Finished in %s", elapsed)
}
//Used to Swipe and like HotGirls if found.
func SwipeAndLike(girls []Girl, likedChannel chan Girl, wg *sync.WaitGroup) {
defer wg.Done()
for _, girl := range girls {
if girl.hot == true {
fmt.Println("::..Liked One..:: **licklick")
likedChannel <- girl
}
}
close(likedChannel)
}
//Used to filter our matches from the girls we liked.
func FindMatches(likedChannel chan Girl, matchedChannel chan Girl, wg *sync.WaitGroup) {
defer wg.Done()
for girl := range likedChannel {
if girl.match == true {
fmt.Println("..::New Match!::..")
matchedChannel <- girl
}
}
close(matchedChannel)
}
//Used to start a conversation with our matches.
func StartConversations(matchedChannel chan Girl, wg *sync.WaitGroup) {
defer wg.Done()
for _ = range matchedChannel {
fmt.Println("..::Started Conversation::..")
}
}
func RandomBool() bool {
return rand.Float32() < 0.5
}
//Used as Mockdata and creates 30 random girls.
func InitGirls(amount int) []Girl {
girls := make([]Girl, 0)
for i := 0; i <= amount; i++ {
g := Girl{RandomBool(), RandomBool()}
girls = append(girls, g)
}
return girls
}