forked from vbauerster/60-days-of-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
switchstatement.go
56 lines (49 loc) · 1.01 KB
/
switchstatement.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
package main
import (
"fmt"
"time"
)
func main() {
// multiple conditionals
// option only exists inside the switch
switch option := 1; option {
// case 1 or 4
case 1, 4:
fmt.Println("option 1 or 4")
case 2:
// fallthrough is the same as "continue to next case without evaluate it"
fmt.Printf("option 2 and ")
fallthrough
case 3:
fmt.Println("option 3")
}
// switch statement "if style"
t := time.Now().Day()
switch {
// if 1 < t <= 15
case t > 1 && t <= 15:
fmt.Println("$$$")
// if 15 < t <= 20
case t > 15 && t <= 20:
fmt.Println("$$$ has gone")
// 20+
default:
fmt.Println("Waiting for the next month")
}
// switch type
// Anonymous function that receive something that implements interface{}(all!)
func(some_var interface{}) {
// case about type
switch some_var.(type) {
// is int
case int:
fmt.Println("Some var is int")
// is string
case string:
fmt.Println("Some var is string")
// None of previous
default:
fmt.Println("Sorry, unknown type!")
}
}(true)
}