/
to.go
284 lines (248 loc) · 5.21 KB
/
to.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package fun
import (
"bytes"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"strconv"
"strings"
"time"
"golang.org/x/text/encoding/ianaindex"
"golang.org/x/text/transform"
)
// Ip2Long 字符串 IP 转整型
func Ip2Long(ipStr string) uint32 {
ip := net.ParseIP(ipStr)
if ip == nil {
return 0
}
ip = ip.To4()
if ip == nil {
return 0
}
return binary.BigEndian.Uint32(ip)
}
// Long2Ip 整型转字符串 IP
func Long2Ip(long uint32) string {
ipByte := make([]byte, 4)
binary.BigEndian.PutUint32(ipByte, long)
ip := net.IP(ipByte)
return ip.String()
}
// ToString 将任意一个类型转换为字符串
func ToString(value any) string {
return fmt.Sprintf("%v", value)
}
// ToInt 数字或字符串转 int 类型
func ToInt(value any) int {
switch v := value.(type) {
case int8:
return int(v)
case uint8:
return int(v)
case uint16:
return int(v)
case int16:
return int(v)
case int32:
return int(v)
case int:
return v
case string:
i, _ := strconv.Atoi(v)
return i
}
return 0
}
// ToLong ToInt64 别名, 数字或字符串转 int64
func ToLong(value any) int64 {
return ToInt64(value)
}
// ToBool 字符串转 bool 类型
func ToBool(str string) bool {
b, err := strconv.ParseBool(str)
if err != nil {
return false
} else {
return b
}
}
// ToUint 数字或字符串转 uint
func ToUint(value any) uint {
switch v := value.(type) {
case int8:
return uint(v)
case uint8:
return uint(v)
case uint16:
return uint(v)
case int16:
return uint(v)
case int32:
return uint(v)
case int:
return uint(v)
case uint:
return v
case string:
i, _ := strconv.ParseUint(v, 10, 32)
return uint(i)
}
return 0
}
// ToUint8 数字或字符串转 uint8
func ToUint8(value any) uint8 {
switch v := value.(type) {
case int8:
return uint8(v)
case uint8:
return v
case string:
i, _ := strconv.ParseUint(v, 10, 8)
return uint8(i)
}
return 0
}
// ToInt64 数字或字符串转 int64
func ToInt64(value any) int64 {
switch v := value.(type) {
case int:
return int64(v)
case uint8:
return int64(v)
case int8:
return int64(v)
case int16:
return int64(v)
case uint16:
return int64(v)
case int32:
return int64(v)
case uint32:
return int64(v)
case int64:
return v
case string:
i, _ := strconv.ParseInt(v, 10, 64)
return i
}
return 0
}
// ToFloat32 数字或字符串转 float32
func ToFloat32(value any) float32 {
switch v := value.(type) {
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
return float32(ToInt64(v))
case float32:
return v
case float64:
return float32(v)
case string:
i, _ := strconv.ParseFloat(v, 32)
return float32(i)
}
return 0
}
// ToFloat64 数字或字符串转 float64
func ToFloat64(value any) float64 {
switch v := value.(type) {
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
return float64(ToInt64(v))
case float32:
return float64(v)
case float64:
return v
case string:
i, _ := strconv.ParseFloat(v, 64)
return i
}
return 0
}
// ToUtf8 指定字符集转 utf-8
func ToUtf8(origin []byte, encode string) ([]byte, error) {
e, err := ianaindex.MIME.Encoding(encode)
if err != nil {
return nil, err
}
if e == nil {
return nil, errors.New("unsupported encoding")
}
r := transform.NewReader(bytes.NewReader(origin), e.NewDecoder())
s, err := io.ReadAll(r)
if err != nil {
return nil, err
}
return s, nil
}
// Utf8To utf-8 转指定字符集
func Utf8To(utf8 []byte, encode string) ([]byte, error) {
e, err := ianaindex.MIME.Encoding(encode)
if err != nil {
return nil, err
}
r := transform.NewReader(bytes.NewReader(utf8), e.NewEncoder())
s, err := io.ReadAll(r)
if err != nil {
return nil, err
}
return s, nil
}
// ToJson 将对象转换为 Json 字符串
func ToJson(object any) string {
res, err := json.Marshal(object)
if err != nil {
return ""
}
return String(res)
}
// ToJsonIndent 将对象转换为 Json 字符串, 带缩进
func ToJsonIndent(object any) string {
res, err := json.MarshalIndent(object, "", " ")
if err != nil {
return ""
}
return String(res)
}
// ToDuration 将数字、字符串转换为 time.Duration,默认是 ns, 如果是字符串,支持 ns,ms,us,s,m,h
func ToDuration(value any) time.Duration {
switch v := value.(type) {
case time.Duration:
return v
case int, int64, int32, int16, int8, uint, uint64, uint32, uint16, uint8:
return time.Duration(ToInt64(v))
case float32, float64:
return time.Duration(ToFloat64(v))
case string:
if strings.ContainsAny(v, "nsuµmh") {
d, _ := time.ParseDuration(v)
return d
} else {
d, _ := time.ParseDuration(v + "ns")
return d
}
}
return 0
}
// ToDurationMs 将数字、字符串转换为 time.Duration,默认是 ms, 如果是字符串,支持 ns,ms,us,s,m,h
func ToDurationMs(value any) time.Duration {
switch v := value.(type) {
case time.Duration:
return v
case int, int64, int32, int16, int8, uint, uint64, uint32, uint16, uint8:
return ToDuration(value) * time.Millisecond
case float32, float64:
return ToDuration(value) * time.Millisecond
case string:
if strings.ContainsAny(v, "nsuµmh") {
d, _ := time.ParseDuration(v)
return d
} else {
d, _ := time.ParseDuration(v + "ms")
return d
}
}
return 0
}