-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathswitch.go
59 lines (54 loc) · 945 Bytes
/
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
package main
import (
"fmt"
"time"
)
func main() {
// Normal
a := 1
switch a {
case 1:
fmt.Println("Equal 1")
case 2:
fmt.Println("Equal 2")
case 5:
fmt.Println("Equal 5")
default:
fmt.Println("No Match")
}
// Multiple match
switch time.Now().Weekday() {
case time.Saturday, time.Sunday:
fmt.Println("It's weekend")
default:
fmt.Println("It's a weekday")
}
// If/else
t := time.Now()
switch {
case t.Hour() > 12:
fmt.Println("Current is", t.Hour()-12, "h AM")
default:
fmt.Println("Current is", t.Hour(), "h AM")
}
// Type Assert
check := func(v interface{}) {
switch t := v.(type) {
case int:
fmt.Println("This is int")
case bool:
fmt.Println("This is bool")
case float32:
fmt.Println("This is float 32")
case float64:
fmt.Println("This is float 64")
default:
fmt.Println("What the hell", t)
}
}
check(1)
check(1 == 1)
check(float32(1.5))
check(1.5)
check("123")
}