-
-
Notifications
You must be signed in to change notification settings - Fork 127
/
Switch.go
73 lines (67 loc) · 1.29 KB
/
Switch.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() {
// Switch Statement
num := 1
switch num {
case 1:
fmt.Println("One")
case 2:
fmt.Println("Two")
default:
fmt.Println("Many")
}
// Switch Statement with multiple cases
switch num {
case 1, 2, 3, 4, 5:
fmt.Println("Some")
case 6, 7, 8, 9:
fmt.Println("More")
default:
fmt.Println("Many")
}
// Switch fallthrough case statement (forces the execution of the next statement)
dayOfWeek := 3
switch dayOfWeek {
case 1:
fmt.Println("Go to work")
fallthrough
case 2:
fmt.Println("Buy some bread")
fallthrough
case 3:
fmt.Println("Visit a friend")
fallthrough
case 4:
fmt.Println("Buy some food")
fallthrough
case 5:
fmt.Println("See your family")
default:
fmt.Println("No information available for that day.")
}
// Switch using conditional statements
switch {
case num < 5:
fmt.Println("Smaller than 5")
case num == 5:
fmt.Println("Five")
case num > 5:
fmt.Println("Bigger than five")
default:
fmt.Println("No information about the number")
}
// Switch initializer statement
switch number := 5; {
case number < 5:
fmt.Println("Smaller than 5")
case number == 5:
fmt.Println("Five")
case number > 5:
fmt.Println("Bigger than five")
default:
fmt.Println("No information about the number")
}
}