-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswitch.go
More file actions
67 lines (54 loc) · 930 Bytes
/
switch.go
File metadata and controls
67 lines (54 loc) · 930 Bytes
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
/*
* Run this example from terminal by
* using this command - go run switch.go
*
* Output:
Write 2 as Two
It's a weekday
It's after noon
I'm a bool
I'm a int
Dont know type string
*/
package main
import "fmt"
import "time"
func main() {
i := 2
fmt.Print("Write ", i, " as ")
switch i {
case 1:
fmt.Println("One")
case 2:
fmt.Println("Two")
case 3:
fmt.Println("Three")
}
switch time.Now().Weekday() {
case time.Saturday, time.Sunday:
fmt.Println("It's the weekend")
default:
fmt.Println("It's a weekday")
}
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 a int")
default:
fmt.Printf("Dont know type %T\n", t)
}
}
whatAmI(true)
whatAmI(1)
whatAmI("Hey")
}