-
Notifications
You must be signed in to change notification settings - Fork 0
/
2_flow_control.go
163 lines (141 loc) · 3.48 KB
/
2_flow_control.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
156
157
158
159
160
161
162
163
// basic flow control statements: for, if, else, switch, defer
package main
import (
"fmt"
"math"
"runtime"
"time"
)
// basic for loop understanding
func forLoops() {
// basic for loop (go only has for loops)
sum := 0
for i := 0; i < 10; i++ {
sum += i
}
fmt.Println(sum)
// for can be like a while loop => optional init and post statements
sum = 1
for sum < 1000 {
sum += sum
}
fmt.Println(sum)
// infinite loop if exit condition not specified
// for {
// }
// for loops and functions
Sqrt(1)
}
// Newton's method: compute sqrt using loop through guesses
func Sqrt(x float64) float64 {
z := x / 2
z_prev := -z
for i := 0; i < 10; i++ {
z -= (z*z - x) / (2 * z)
// break if value not changing
if z == z_prev {
fmt.Println("Exiting loop")
break
}
z_prev = z
fmt.Println(z)
}
fmt.Printf("The sqrt of %g is ~%g\n", x, z)
return z
}
// basic if/else understanding
func ifElse() {
fmt.Println(sqrt(2), sqrt(-4))
// Both calls to pow return their results before the call to fmt.Println begins
fmt.Println(
pow(3, 2, 10),
pow(3, 3, 20),
)
}
// get sqrt of float as string
func sqrt(x float64) string {
// irrational number
if x < 0 {
return sqrt(-x) + "i"
}
// Sprint converts number to string
return fmt.Sprint(math.Sqrt(x))
}
// get biggest number between x^n or lim and return it
func pow(x, n, lim float64) float64 {
// if statement can have short statement to execute before start of condition
if v := math.Pow(x, n); v < lim {
return v
} else {
// will print out before the numbers, since pow calls complete before it is printed out in if_else()
fmt.Printf("%g >= %g\n", v, lim)
}
return lim
}
// basic switch understanding
func switchStatements() {
fmt.Println("Go runs on ")
// switch cases do not need to be integers
switch os := runtime.GOOS; os {
case "darwin":
fmt.Println("OS X.")
// do not need break statement (automatically added)
case "linux":
fmt.Println("Linux.")
default:
// freebsd, openbsd,
// plan9, windows...
fmt.Printf("%s.\n", os)
}
// switch cases do not need to be constants
fmt.Println("When's Saturday?")
today := time.Now().Weekday()
switch time.Saturday {
case today:
fmt.Println("Today.")
case today + 1:
fmt.Println("Tomorrow.")
case today + 2:
fmt.Println("In two days.")
default:
fmt.Println("Too far away.")
}
// switch with no conditions is the same as switch true (ideal for long if-then-else chains)
t := time.Now()
switch {
case t.Hour() < 12:
fmt.Println("Good morning.")
case t.Hour() < 17:
fmt.Println("Good afternoon.")
default:
fmt.Println("Good evening.")
}
}
// basic understanding of defer statements
func deferStatements() {
// A defer statement defers the execution of a function until the surrounding function returns.
// The deferred call's arguments are evaluated immediately, but the function call is not executed
// until the surrounding function returns.
// will only execute after surrounding function (hello and stack_defer) is done
defer fmt.Println("world")
fmt.Println("hello")
// stacking defers
stackDefer()
}
// Deferred function calls are pushed onto a stack.
// When a function returns, its deferred calls are executed in last-in-first-out order.
func stackDefer() {
fmt.Println("counting")
for i := 0; i < 10; i++ {
// will print in revers order
defer fmt.Println(i)
}
fmt.Println("done")
}
func main() {
fmt.Println("Flow control statements: for, if, else, switch, defer")
forLoops()
ifElse()
switchStatements()
deferStatements()
}