-
Notifications
You must be signed in to change notification settings - Fork 2
/
trafficLight.go
55 lines (44 loc) · 1.32 KB
/
trafficLight.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"
)
/***********************************************************************
drivingNorth and turningLeft both infinitely send their rude gestures
to the the channel that was passed as a function parameter
Let's say every 3 seconds the traffic light changes...
Let's add some functionality to the code bellow to alert us when
an accident occurs...and...you know...prevent them from happenning
*************************************************************************/
func drivingNorth(greenLight chan<- string) {
for {
greenLight <- "BEEP BEEP, GOING NORTH PAL, OUTTA MY WAY!!!"
time.Sleep(time.Second)
}
}
func turningLeft(greenLight chan<- string) {
for {
greenLight <- "DON'T CALL ME PAL BUDDY, I'M TURNING LEFT!"
time.Sleep(time.Second)
}
}
func trafficIntersection(light1 chan string, light2 chan string) {
for {
select {
case northRudeGesture := <- light1:
fmt.Println(northRudeGesture)
case leftRudeGesture := <- light2:
fmt.Println(leftRudeGesture)
}
}
}
func main() {
northGreenLight := make(chan string)
leftGreenLight := make(chan string)
go drivingNorth(northGreenLight)
go turningLeft(leftGreenLight)
go trafficIntersection(northGreenLight,leftGreenLight)
// Ignore
var input string
fmt.Scanln(&input)
}