-
Notifications
You must be signed in to change notification settings - Fork 0
/
conditionals.go
65 lines (56 loc) · 1.01 KB
/
conditionals.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
package main
import (
"fmt"
"time"
)
func main() {
//For statements
z := 1
for z < 5 {
fmt.Println(z)
z = z + 1
}
// if and for combinations
for i := 10; i > 6; i-- {
if i%2 == 0 {
fmt.Println(i, " is an even number")
} else {
continue
}
}
//switch statements
t := 4
switch t { //one with expression
case 3:
fmt.Println("three")
t++
case 4:
fmt.Println("four")
t--
}
switch { //one without expression
case t > 5:
fmt.Println("big ", t)
case t < 5:
fmt.Println("small ", t)
}
t_time := time.Now().Weekday()
switch t_time {
case time.Monday, time.Tuesday, time.Wednesday, time.Thursday, time.Friday:
fmt.Println("Its a weekday")
default:
fmt.Println("Its a weekend")
}
//defer function statements
for i:=1; i<5;i++{
defer fmt.Println(i)
}
fmt.Println("Finished counting:")
//lets see some miracle with defer
defer func (){
str:= recover()
fmt.Println(str)
}()
panic("Hello There")
//panic and recover are predefined functions similar to try and catch
}