1 Star 1 Fork 0

liangchao/gx

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
basic.go 1.59 KB
一键复制 编辑 原始数据 按行查看 历史
liangchao 提交于 2025-07-06 23:26 +08:00 . 增加MustParseInt
package gx
import (
"fmt"
"reflect"
"strconv"
)
// 获取值指针
func Ptr[T interface{}](value T) *T {
return &value
}
// 判断是否nil,支持指针
func IsNil(v any) bool {
if v == nil {
return true
}
return fmt.Sprintf("%v", v) == "<nil>"
}
// 判断值是否为true
func IsTrue(value any) bool {
if v, ok := value.(bool); ok {
return v
} else if v, ok := value.(*bool); ok {
return *v
}
return false
}
// 判断值是否为False
func IsFalse(value any) bool {
if v, ok := value.(bool); ok {
return !v
} else if v, ok := value.(*bool); ok {
return !*v
}
return false
}
// 比较值,如果是指针类型,会转换为值再比较
func CompareValue(x any, y any) bool {
xstr := fmt.Sprintf("%v", GetValue(x))
ystr := fmt.Sprintf("%v", GetValue(y))
return xstr == ystr
}
// 判断数据是否为指针类型
func IsPointer(v interface{}) bool {
return reflect.ValueOf(v).Kind() == reflect.Pointer
}
// 判断对象是否map
func IsMap(v interface{}) bool {
return reflect.ValueOf(v).Kind() == reflect.Map
}
// 判断值是否为切片类型
func IsSlice(v interface{}) bool {
return reflect.ValueOf(v).Kind() == reflect.Slice
}
// 判断数据是否为结构体类型
func IsStruct(v interface{}) bool {
return reflect.TypeOf(v).Kind() == reflect.Struct
}
// 获取值,如果是指针类型,会返回其所指的值
func GetValue(v interface{}) interface{} {
if IsPointer(v) {
return reflect.ValueOf(v).Elem()
} else {
return v
}
}
func MustParseInt(s string) int64 {
value, err := strconv.ParseInt(s, 10, 64)
if err != nil {
panic(err)
}
return value
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/makitdone/gx.git
git@gitee.com:makitdone/gx.git
makitdone
gx
gx
v1.0.1

搜索帮助