-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControlFlow.go
More file actions
70 lines (55 loc) · 1.05 KB
/
Copy pathControlFlow.go
File metadata and controls
70 lines (55 loc) · 1.05 KB
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
package main
import (
"fmt"
)
func main() {
populationMap := map[string]int{
"India": 12347859,
"US": 1234,
"UK": 15369,
}
// i is only defined in if block
if i, ok := populationMap["India"]; ok {
fmt.Println(i)
}
// in case of multiple conditions in a if statement , short cicuiting takes place
// switch case in go
// i := 10
switch i := 3; i {
case 1, 5, 6:
fmt.Println(1)
case 2:
fmt.Println(2)
default:
fmt.Println("0")
}
i := 5
// first case will be executed (different than the break concept in other languages)
//fallthrough executes the next case irrespective of the condition
switch {
case i <= 10:
fmt.Println("executed1")
fallthrough
case i <= 20:
fmt.Println("executed2")
}
for i, j := 0, 0; i < 10; i, j = i+3, j+2 {
fmt.Println(i, j)
}
// break and continue in for loop
for {
if i%2 < 2 {
break
} else {
continue
}
}
// range for arrays and maps
s := []int{1, 2, 3, 4}
for k, v := range s {
fmt.Println(k, v)
}
for k, v := range populationMap {
fmt.Println(k, v)
}
}