-
Notifications
You must be signed in to change notification settings - Fork 0
/
switch.go
52 lines (48 loc) · 863 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
package main
import (
"fmt"
"time"
)
func main() {
i := 2
fmt.Println("write", i, "as")
//only case
switch i {
case 0:
fmt.Println("zero")
case 1:
fmt.Println("one")
case 2:
fmt.Println("two")
case 3:
fmt.Println("three")
}
// case and default
switch time.Now().Weekday() {
case time.Sunday, time.Saturday:
fmt.Println("It's the weekend")
default:
fmt.Println("It's the weekday")
}
//swtich无参数
t := time.Now()
switch {
case t.Hour() < 12:
fmt.Println("It's before noon")
default:
fmt.Println("It's after noon")
}
whatAmI := func(i interface{}) {
switch t := i.(type) {
case bool:
fmt.Println("I'm a bool")
case int:
fmt.Println("I'm an int")
default:
fmt.Printf("Don't know type , %T\n", t)
}
}
whatAmI(true)
whatAmI(1)
whatAmI("hello")
}