-
Notifications
You must be signed in to change notification settings - Fork 1
/
string.go
46 lines (41 loc) · 954 Bytes
/
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
package convert
import (
"fmt"
"reflect"
)
// 强制转换一个对象为string
func MustString(value interface{}) string {
switch v := value.(type) {
case fmt.Stringer:
return v.String()
case string:
return v
default:
return fmt.Sprintf("%v", value)
}
}
// 转换一个对象为string
func ToString(value interface{}) (string, error) {
// fake方法用于统一接口
return MustString(value), nil
}
// 强转一个字符串数组
func MustStringArray(array interface{}) (resArray []string) {
t := reflect.TypeOf(array)
switch t.Kind() {
case reflect.Array, reflect.Slice:
{
v := reflect.ValueOf(array)
resArray = make([]string, v.Len())
for index, _ := range resArray {
resArray[index] = MustString(v.Index(index).Interface())
}
return
}
}
return []string{MustString(array)}
}
// 尝试一个字符串数组
func ToStringArray(value interface{}) ([]string, error) {
return MustStringArray(value), nil
}