/
interfacelib.go
155 lines (118 loc) · 2.4 KB
/
interfacelib.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package interfacelib
import (
"strconv"
)
func ToFloat64(params interface{}) (res float64) {
switch params.(type) {
case int64:
t, _ := params.(int64)
res = float64(t)
case int32:
t, _ := params.(int32)
res = float64(t)
case int16:
t, _ := params.(int16)
res = float64(t)
case int8:
t, _ := params.(int8)
res = float64(t)
case int:
t, _ := params.(int)
res = float64(t)
case uint:
t, _ := params.(uint)
res = float64(t)
case float64:
res, _ = params.(float64)
case float32:
t, _ := params.(float32)
res = float64(t)
case string:
str, _ := params.(string)
res, _ = strconv.ParseFloat(str, 64)
default:
res = 0
}
return res
}
func ToInt64(params interface{}) (res int64) {
switch params.(type) {
case int64:
res, _ = params.(int64)
case int32:
t, _ := params.(int32)
res = int64(t)
case int16:
t, _ := params.(int16)
res = int64(t)
case int8:
t, _ := params.(int8)
res = int64(t)
case int:
t, _ := params.(int)
res = int64(t)
case uint:
t, _ := params.(uint)
res = int64(t)
case float64:
t, _ := params.(float64)
res = int64(t)
case float32:
t, _ := params.(float32)
res = int64(t)
case string:
t, _ := params.(string)
res, _ = strconv.ParseInt(t, 10, 64)
default:
res = 0
}
return res
}
func ToString(params interface{}) (str string) {
switch params.(type) {
case int64:
t, _ := params.(int64)
str = strconv.FormatInt(int64(t), 10)
case int32:
t, _ := params.(int32)
str = strconv.FormatInt(int64(t), 10)
case int16:
t, _ := params.(int16)
str = strconv.FormatInt(int64(t), 10)
case int8:
t, _ := params.(int8)
str = strconv.FormatInt(int64(t), 10)
case int: // S
t, _ := params.(int)
str = strconv.FormatInt(int64(t), 10)
case uint:
t, _ := params.(uint)
str = strconv.FormatInt(int64(t), 10)
case []uint8:
t, _ := params.([]uint8)
var ba []byte
for _, b := range t {
ba = append(ba, byte(b))
}
str = string(ba)
case float64:
t, _ := params.(float64)
str = strconv.FormatFloat(t, 'E', -1, 64)
case float32:
t, _ := params.(float32)
str = strconv.FormatFloat(float64(t), 'E', -1, 64)
case string:
str, _ = params.(string)
default:
str = ""
}
return str
}
func StringToFloat64(params interface{}) float64 {
fv, _ := params.(float64)
return fv
}
func StringToInt64(params interface{}) int64 {
fv, _ := strconv.ParseInt(ToString(params), 10, 64)
return fv
}