-
Notifications
You must be signed in to change notification settings - Fork 0
/
Go_Select.go
50 lines (41 loc) · 1.36 KB
/
Go_Select.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
//Every stand-alone go program must have a main package
package main
//fmt package - to print out statements
import "fmt"
//time package - to deal with time
import "time"
//os package - to perform OS functionalities
import"os"
//Function of utilizing select block
//Select block is similar to switch but used in case of channels
func Select_func(c chan int, end chan string){
//Infinite for is used to use all the values in the select block
//If not used, only first value will be used
for{
select{
case <-c : fmt.Println("Received")
case <-end : fmt.Println("End")
//To exit from the program after receiving the end signal
//If not used, leads to deadlock, as we are using select{} in main()
os.Exit(0)
}
}
}
//Entry point for the program
func main() {
//Making two unbuffered channels
c := make(chan int)
end := make(chan string)
go func(){
c <- 1
end <- "END"
}()
//When using 2 or more Go Routines, give a sleep time of 1 second after every Go Routine to compensate for concurrency
time.Sleep(time.Second * 1)
go Select_func(c, end)
time.Sleep(time.Second * 1)
//SELECT - Waits Indefinitely by halting main statement from finishing
//User has to interrupt to end execution, else the program will be keep on running
//Returns deadlock - If For loop is finite
select{}
}