Files
mayfly-go/server/pkg/utils/any.go
2023-05-24 12:32:17 +08:00

55 lines
958 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package utils
import (
"strconv"
)
// any类型转换为string, 如果any为nil则返回空字符串
func Any2String(val any) string {
if value, ok := val.(string); !ok {
return ""
} else {
return value
}
}
// any类型转换为int可将字符串或int64转换, 如果any为nil则返回0
func Any2Int(val any) int {
switch value := val.(type) {
case int:
return value
case string:
if intV, err := strconv.Atoi(value); err == nil {
return intV
}
case int64:
return int(value)
case uint64:
return int(value)
case int32:
return int(value)
case uint32:
return int(value)
case int16:
return int(value)
case uint16:
return int(value)
case int8:
return int(value)
case uint8:
return int(value)
default:
return 0
}
return 0
}
// any类型转换为int64, 如果any为nil则返回0
func Any2Int64(val any) int64 {
if value, ok := val.(int64); !ok {
return int64(Any2Int(val))
} else {
return value
}
}