45 lines
1.3 KiB
Go
45 lines
1.3 KiB
Go
package convx
|
|
|
|
import (
|
|
"errors"
|
|
"strconv"
|
|
)
|
|
|
|
// interface 转 string
|
|
func ToString(val interface{}) (str string, err error) {
|
|
|
|
var s string
|
|
if vv, ok := val.(float32); ok {
|
|
s = strconv.FormatFloat(float64(vv), 'f', -1, 32)
|
|
} else if vv, ok := val.(float64); ok {
|
|
s = strconv.FormatFloat(vv, 'f', -1, 64)
|
|
} else if vv, ok := val.(int); ok {
|
|
s = strconv.Itoa(vv)
|
|
} else if vv, ok := val.(int32); ok {
|
|
s = strconv.Itoa(int(vv))
|
|
} else if vv, ok := val.(int64); ok {
|
|
s = strconv.FormatInt(vv, 10)
|
|
} else if vv, ok := val.(string); ok {
|
|
s = vv
|
|
} else if vv, ok := val.(bool); ok {
|
|
s = strconv.FormatBool(vv)
|
|
} else if vv, ok := val.(uint); ok {
|
|
s = strconv.FormatUint(uint64(vv), 10)
|
|
} else if vv, ok := val.(uint32); ok {
|
|
s = strconv.FormatUint(uint64(vv), 10)
|
|
} else if vv, ok := val.(uint64); ok {
|
|
s = strconv.FormatUint(vv, 10)
|
|
} else if vv, ok := val.(uint8); ok {
|
|
s = strconv.FormatUint(uint64(vv), 10)
|
|
} else if vv, ok := val.(uint16); ok {
|
|
s = strconv.FormatUint(uint64(vv), 10)
|
|
} else if vv, ok := val.(int8); ok {
|
|
s = strconv.FormatInt(int64(vv), 10)
|
|
} else if vv, ok := val.(int16); ok {
|
|
s = strconv.FormatInt(int64(vv), 10)
|
|
} else {
|
|
return s, errors.New("不支持的参数类型")
|
|
}
|
|
return s, nil
|
|
}
|