-
Notifications
You must be signed in to change notification settings - Fork 2
/
conv.go
53 lines (46 loc) · 849 Bytes
/
conv.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
package convs
import (
"reflect"
"strconv"
)
////////////////////////////////////////////////////////////////////////////////
//
func ToInt(v interface{}) int {
switch v.(type) {
case int:
return v.(int)
case float64:
return int(v.(float64))
case string:
d, _ := strconv.Atoi(v.(string))
return d
case int32:
return int(v.(int32))
case float32:
return int(v.(float32))
case int64:
return int(v.(int64))
}
return 0
}
//
func ToFloat64(v interface{}) float64 {
switch v.(type) {
case float64:
return v.(float64)
case string:
r, _ := strconv.ParseFloat(v.(string), 64)
return r
}
return 0.0
}
//
func ToInterfaceSlice(val interface{}) []interface{} {
rv := reflect.ValueOf(val)
num := rv.Len()
ret := make([]interface{}, num)
for i := 0; i < num; i++ {
ret[i] = rv.Index(i).Interface()
}
return ret
}