-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
59 lines (51 loc) · 964 Bytes
/
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 switches
import (
"fmt"
"time"
)
/*
Expressing conditionals across multiple branches
*/
func Run() {
basicSwitch()
switchGroupingCases()
defaultSwitch()
}
func basicSwitch() {
i := 0
switch i {
case 0:
fmt.Println("Zero")
case 1:
fmt.Println("Two")
}
}
func switchGroupingCases() {
today := time.Now().Weekday()
switch today {
case time.Sunday, time.Saturday:
fmt.Println("It is a weekend!")
case time.Monday:
fmt.Println("Monday")
case time.Tuesday, time.Wednesday, time.Thursday, time.Friday:
fmt.Println("It is not monday or a weekend.")
}
}
func defaultSwitch() {
typeDetector := func(i any) {
switch t := i.(type) {
case bool:
fmt.Println("I'm a boolean!")
case int:
fmt.Println("I'm an int!")
case string:
fmt.Println("I'm a string!")
default:
fmt.Printf("I didn't match any cases, unknown type %T\n", t)
}
}
typeDetector(true)
typeDetector(100)
typeDetector("foo")
typeDetector(nil)
}