-
Notifications
You must be signed in to change notification settings - Fork 6
/
string.go
83 lines (77 loc) · 1.81 KB
/
string.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
package gox
import (
"encoding/json"
"fmt"
"strconv"
)
var _ = ToStrings
// ToStrings 将任意类型转换成字符串列表
func ToStrings(from ...any) (to []string) {
to = make([]string, 0, len(from))
for _, value := range from {
to = append(to, ToString(value))
}
return
}
// ToString 将任意类型转换为字符串
func ToString(from any) (to string) {
switch val := from.(type) {
case int8:
to = strconv.FormatInt(int64(val), 10)
case *int8:
to = strconv.FormatInt(int64(*val), 10)
case uint8:
to = strconv.FormatUint(uint64(val), 10)
case *uint8:
to = strconv.FormatUint(uint64(*val), 10)
case int:
to = strconv.FormatInt(int64(val), 10)
case *int:
to = strconv.FormatInt(int64(*val), 10)
case uint:
to = strconv.FormatUint(uint64(val), 10)
case *uint:
to = strconv.FormatUint(uint64(*val), 10)
case int32:
to = strconv.FormatInt(int64(val), 10)
case *int32:
to = strconv.FormatInt(int64(*val), 10)
case uint32:
to = strconv.FormatUint(uint64(val), 10)
case *uint32:
to = strconv.FormatUint(uint64(*val), 10)
case int64:
to = strconv.FormatInt(val, 10)
case *int64:
to = strconv.FormatInt(*val, 10)
case uint64:
to = strconv.FormatUint(val, 10)
case *uint64:
to = strconv.FormatUint(*val, 10)
case float32:
to = strconv.FormatFloat(float64(val), 'f', -1, 64)
case *float32:
to = strconv.FormatFloat(float64(*val), 'f', -1, 64)
case float64:
to = strconv.FormatFloat(val, 'f', -1, 64)
case *float64:
to = strconv.FormatFloat(*val, 'f', -1, 64)
case bool:
to = strconv.FormatBool(val)
case *bool:
to = strconv.FormatBool(*val)
case string:
to = val
case *string:
to = *val
case fmt.Stringer:
to = val.String()
default:
if bytes, err := json.Marshal(from); nil == err {
to = string(bytes)
} else {
to = fmt.Sprintf("%v", from)
}
}
return
}