Files
workspace/code/base-project/base-go-v2/internal/utils/strutil/strutil.go
T
2025-10-31 22:35:20 +08:00

54 lines
1013 B
Go

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)
}
}