-
Notifications
You must be signed in to change notification settings - Fork 0
/
ifAndSwitch.go
155 lines (138 loc) · 3 KB
/
ifAndSwitch.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import "fmt"
func ifInGolang() {
/*
Simplest if condition
*/
if true {
fmt.Println("I am true")
}
/*
We can also have initializer if condition. The variables
from the initializer are scoped to the if block.
*/
myMarks := map[string]int{
"Science": 85,
"Maths": 100,
}
if science, ok := myMarks["Science"]; ok {
fmt.Println(science, ok)
}
/*
Logical operators:
>, <, ==, >=, <=, !=, !, &&, ||
*/
/*
Short circuiting.
Golang runs the condition lazily. When it finds that
num1 is greater than 10, it enters the if block and does
not computer to check if the other conditions are true or
not. This is because, it will have to enter the if block and
any other condition is irrelavant.
For the second condition, it does calculate getTrueResult() and
we see the print statement. This is because, the first case
is false.
*/
const (
num1 uint = 50
num2 uint = 60
)
if num1 > 10 || getTrueResult() || num2 > 10 {
fmt.Println("condition 1")
}
if num1 < 10 || getTrueResult() || num2 > 10 {
fmt.Println("condition 2")
}
}
func switchInGolang() {
/*
Simplest switch case
*/
switch 10 {
case 1:
fmt.Println("1")
case 2:
fmt.Println("2")
default:
fmt.Println("default")
}
/*
We cannot have multiple cases for a single block like in other languages,
however, we can have multiple tests in the same case as such
*/
switch 10 {
case 1, 3, 5, 7, 9, 11:
fmt.Println("1")
case 2, 4, 6, 8, 10:
fmt.Println("2")
default:
fmt.Println("default")
}
num := 10
switch cond := num % 2; cond { // simply n % 2
case 1:
fmt.Println("1")
case 0:
fmt.Println("2")
default:
fmt.Println("default")
}
/*
We can also have a switch where we put no condition
infront of it
*/
num2 := 10
switch { // simply n % 2
case num2%2 == 1:
fmt.Println("1")
case num2%2 == 0:
fmt.Println("2")
default:
fmt.Println("default")
}
/*
There is no need for adding 'break' because it is implied by
default. But 'break' keyword does exist in Goland and we can
use it if we want to exit a case
*/
/*
There is another keyword in context of switch called 'fallthrough'.
Fallthrough is weird in its behaviour. If we use fallthrough, the
condition for the case will not be evaluated and the statement in the
next case will be run no matter what. It cannot be used conditionally
as well.
https://stackoverflow.com/questions/45268681/golangs-fallthrough-seems-unexpected
The default behaviour in other languages is to fallthrough
*/
num3 := 11
switch { // simply n % 2
case num3%2 == 1:
fmt.Println("1")
fallthrough
case num3%2 == 0:
fmt.Println("fallthrough")
default:
fmt.Println("default")
}
}
func typeSwitch() {
var i interface{} = 5
switch i.(type) {
case int:
fmt.Println("int")
case string:
fmt.Println("string")
case float32:
fmt.Println("float")
}
}
func ifAndSwitch() {
fmt.Println("ifAndSwitch")
ifInGolang()
switchInGolang()
typeSwitch()
}
func getTrueResult() bool {
fmt.Println("getTrueResult")
return true
}