-
Notifications
You must be signed in to change notification settings - Fork 0
/
loops.go
80 lines (71 loc) · 1.37 KB
/
loops.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
package main
func loops() {
/*
// loop till condition
var i int
for i < 5 {
println(i)
i++
if i == 4 {
// "continue" exits out of the current loop iteration
continue
}
if i == 3 {
// "break" command exits out of the entire loop
break
}
println("Continuing...")
}
*/
/*
// Loop with condition and Post clause
for i := 0; i < 5; i++ {
// i is only available within scope of for loop
println(i)
}
*/
/*
// infinite loop, only exits due to if statement
var i int
for {
if i == 5 {
break
}
println(i)
i++
}
*/
/*
// loops through entire slice
slice := []int{1, 2, 3}
for i := 0; i < len(slice); i++ {
println(slice[i])
}
*/
/*
// manually loop over collection
slice := []int{1, 2, 3}
for i, v := range slice {
println(i, v)
}
*/
/*
// loop over collection using range using key and value
wellKnownPorts := map[string]int{"http": 80, "https": 443}
for k, v := range wellKnownPorts {
println(k, v)
}
*/
/*
// loop over collection and use only first variable
wellKnownPorts := map[string]int{"http": 80, "https": 443}
for k := range wellKnownPorts {
println(k)
}
*/
// loop over collection using 2nd variable, by using the write only variable
wellKnownPorts := map[string]int{"http": 80, "https": 443}
for _, v := range wellKnownPorts {
println(v)
}
}