Ai
4 Star 17 Fork 8

NightTC/Gobige

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Math.go 1.61 KB
一键复制 编辑 原始数据 按行查看 历史
package common
// NumberT 泛型约束,支持的数值类型
type NumberT interface {
int8 | int16 | int32 | int64 | int | byte | uint16 | uint32 | uint64 | uint | float32 | float64
}
// Max 返回两个数值中的较大值。
func Max[T NumberT](a, b T) T {
if a > b {
return a
}
return b
}
// Min 返回两个数值中的较小值。
func Min[T NumberT](a, b T) T {
if a < b {
return a
}
return b
}
// ConvertInt64 将任意支持的类型转换为 int64。
func ConvertInt64(a interface{}) int64 {
switch v := a.(type) {
case string:
return NewString(v).ToInt64V()
case int8:
return int64(v)
case int16:
return int64(v)
case int32:
return int64(v)
case int64:
return v
case int:
return int64(v)
case byte:
return int64(v)
case uint16:
return int64(v)
case uint32:
return int64(v)
case uint64:
return int64(v)
case uint:
return int64(v)
case float32:
return int64(v)
case float64:
return int64(v)
default:
return 0
}
}
// ConvertInt32 将任意支持的类型转换为 int32。
func ConvertInt32(a interface{}) int32 {
switch v := a.(type) {
case string:
return int32(NewString(v).ToInt64V())
case int8:
return int32(v)
case int16:
return int32(v)
case int32:
return v
case int64:
return int32(v)
case int:
return int32(v)
case byte:
return int32(v)
case uint16:
return int32(v)
case uint32:
return int32(v)
case uint64:
return int32(v)
case uint:
return int32(v)
case float32:
return int32(v)
case float64:
return int32(v)
default:
return 0
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/night-tc/gobige.git
git@gitee.com:night-tc/gobige.git
night-tc
gobige
Gobige
3e11984fb1ba

搜索帮助