-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
59 lines (50 loc) · 1.01 KB
/
main.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
package forloop
import (
"fmt"
)
/* In the pursuit of simplicity, go only has a single looping construct.
the concept of for each does not really exist and while loops are also
implemented using the for construct.
Golang also does not offer the for-else idiom.
*/
func Run() {
// A Basic for loop
basicForLoop()
// An infinite loop that demonstrates how to break
infiniteLoopWithBreak()
// A basic for loop that utilises continue conditionally
continuationInLooping()
// An implementation of a while loop
whileLoop()
}
func basicForLoop() {
defer func() { fmt.Println("Basic for loop has finished.") }()
for i := 0; i < 10; i++ {
fmt.Println(i)
}
}
func infiniteLoopWithBreak() {
i := 1
for {
if i == 100 {
break
}
i++
}
}
func continuationInLooping() {
for j := 0; j < 10; j++ {
if j%2 == 0 {
fmt.Printf("Skipping because %d is even\n", j)
continue
}
}
}
func whileLoop() {
hits := 0
for hits < 10 {
fmt.Println("Still false...")
hits++
}
fmt.Println("We finally got out!")
}