forked from cdarwin/go-koans
-
Notifications
You must be signed in to change notification settings - Fork 0
/
about_control_flow.go
71 lines (61 loc) · 1.17 KB
/
about_control_flow.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
package go_koans
import "fmt"
func aboutControlFlow() {
{
a, b, c := 1, 2, 3
assert(a == __int__) // multiple assignment
assert(b == __int__) // can make
assert(c == __int__) // life easier
}
var str string
{
if 3.14 == 3 {
str = "what is love?"
} else {
str = "baby dont hurt me"
}
assert(str == __string__) // no more
if length := len(str); length == 17 {
str = "to be"
} else {
str = "or not"
}
assert(str == __string__) // that is the question
}
{
hola1, hola2 := "ho", "la"
switch "hello" {
case "hello":
str = "hi"
case "world":
str = "planet"
case fmt.Sprintf("%s%s", hola1, hola2):
str = "senor"
}
assert(str == __string__) // cases can be of any type, even arbitrary expressions
switch {
case false:
str = "first"
case true:
str = "second"
}
assert(str == __string__) // in the absence of value, there is truth
}
{
n := 0
for i := 0; i < 5; i++ {
n += i
}
assert(n == __int__) // for can have the structure with which we are all familiar
}
{
n := 1
for {
n *= 2
if n > 20 {
break
}
}
assert(n == __int__) // though omitting everything creates an infinite loop
}
}