u
This commit is contained in:
1 parent
91ae4cb474
commit
b116690769
14 files changed
+669
-56
No files matched your search
@@ -0,0 +1,53 @@
|
||||
package strutil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// ToString 通用高效转换为 string
|
||||
func ToString(v any) string {
|
||||
switch n := v.(type) {
|
||||
// 整数类型
|
||||
case int:
|
||||
return strconv.FormatInt(int64(n), 10)
|
||||
case int8:
|
||||
return strconv.FormatInt(int64(n), 10)
|
||||
case int16:
|
||||
return strconv.FormatInt(int64(n), 10)
|
||||
case int32:
|
||||
return strconv.FormatInt(int64(n), 10)
|
||||
case int64:
|
||||
return strconv.FormatInt(n, 10)
|
||||
|
||||
// 无符号整数
|
||||
case uint:
|
||||
return strconv.FormatUint(uint64(n), 10)
|
||||
case uint8:
|
||||
return strconv.FormatUint(uint64(n), 10)
|
||||
case uint16:
|
||||
return strconv.FormatUint(uint64(n), 10)
|
||||
case uint32:
|
||||
return strconv.FormatUint(uint64(n), 10)
|
||||
case uint64:
|
||||
return strconv.FormatUint(n, 10)
|
||||
|
||||
// 浮点数
|
||||
case float32:
|
||||
return strconv.FormatFloat(float64(n), 'f', -1, 32)
|
||||
case float64:
|
||||
return strconv.FormatFloat(n, 'f', -1, 64)
|
||||
|
||||
// 字符串
|
||||
case string:
|
||||
return n
|
||||
|
||||
// 布尔
|
||||
case bool:
|
||||
return strconv.FormatBool(n)
|
||||
|
||||
default:
|
||||
// fallback
|
||||
return fmt.Sprintf("%v", v)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user