-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathifelse.go
73 lines (58 loc) · 1.14 KB
/
ifelse.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
package main
import "fmt"
func main() {
i := 1
// Basic
if i > 10 {
fmt.Println(i)
} else if i == 10 {
fmt.Println(i, "is 10")
} else {
fmt.Println(i, "is less than 10")
}
// if with statement before condition
if i = 10; i > 10 {
fmt.Println(i)
} else if i == 10 {
fmt.Println(i, "is equal 10")
} else {
fmt.Println(i, "is less than 10")
}
fmt.Println("")
/* ------------------------------------------------------ */
// Switch
os := "Windows"
switch os {
case "darwin":
fmt.Println("Mac OS Hipster")
// cases break automatically, no fallthrough by default
case "linux":
fmt.Println("Linux Geek")
default:
fmt.Println("Other")
}
// Comparisons in switch cases
number := 42
switch {
case number < 42:
fmt.Println("Smaller")
case number == 42:
fmt.Println("Equal")
case number > 42:
fmt.Println("Greater")
}
fmt.Println("")
/* ------------------------------------------------------ */
// Type Switch
var foo interface{} = 1
switch foo.(type) {
case int:
fmt.Println("int")
case float64:
fmt.Println("Float64")
case string:
fmt.Println("string")
default:
fmt.Println("other type")
}
}