-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0.typeswitch.go
69 lines (60 loc) · 1.54 KB
/
0.typeswitch.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
60
61
62
63
64
65
66
67
68
69
package main
import (
"fmt"
"reflect"
)
func main(){
a := []interface{}{1, true, "bharath"}
typeSwitch(a)
b := []bool{true, false, true}
typeSwitch(b)
fmt.Println("-----------Type Switch Use Case Example-----------")
v1 := []interface{}{1, true, "bharath", []interface{}{2, false, "Talluri",
[]interface{}{3, true, "Venkata"}}}
fmt.Printf("v1 = %v, \nGo Representation = %#[1]v\n", v1)
fmt.Println("reflect.TypeOf(a[3]) == reflect.TypeOf([]interface{}{}):",
reflect.TypeOf(v1[3]) == reflect.TypeOf([]interface{}{}))
rslt := embedAccess(v1, 3, 3)
fmt.Printf("embedAccess(v, 3, 3) = %v, of type %[1]T\n", rslt)
}
func typeSwitch(x interface{}) {
switch v := x.(type) {
case int:
fmt.Println("int:", v)
case float64:
fmt.Println("float64:", v)
case string:
fmt.Println("string:", v)
case bool:
fmt.Println("bool:", v)
case []int:
fmt.Println("[]int:", v)
case []float64:
fmt.Println("[]float64:", v)
case []string:
fmt.Println("[]string:", v)
case []bool:
fmt.Println("[]bool:", v)
case []interface{}:
fmt.Println("[]interface{}:", v)
default:
fmt.Println("unknown")
}
}
func typeSwitchSample(x interface{}) []interface{}{
var value []interface{}
switch v := x.(type) {
case []interface{}:
value = v
default:
fmt.Println("unknown")
}
return value
}
func embedAccess(root []interface{}, items ...int) []interface{}{
iterVal := root
for _, val := range items {
iterVal = typeSwitchSample(iterVal[val])
}
return iterVal
}