This commit is contained in:
Yun
2023-12-03 16:20:46 +08:00
commit 3d048cb9b6
9 changed files with 430 additions and 0 deletions
+44
View File
@@ -0,0 +1,44 @@
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
}