dtp/util/ptrreader.go

107 lines
1.6 KiB
Go

package util
import "unsafe"
type eface struct {
v int64
ptr unsafe.Pointer
}
const (
DefaultBool bool = false
DefaultString string = ""
DefaultInt int = 0
DefaultInt32 int32 = 0
DefaultInt64 int64 = 0
DefaultFloat32 float32 = 0
DefaultFloat64 float64 = 0
)
func DerefBool(p *bool) bool {
if p != nil {
return *p
}
return DefaultBool
}
func DerefString(p *string) string {
if p != nil {
return *p
}
return DefaultString
}
func DerefInt(p *int) int {
if p != nil {
return *p
}
return DefaultInt
}
func DerefInt32(p *int32) int32 {
if p != nil {
return *p
}
return DefaultInt32
}
func DerefInt64(p *int64) int64 {
if p != nil {
return *p
}
return DefaultInt64
}
func DerefFloat32(p *float32) float32 {
if p != nil {
return *p
}
return DefaultFloat32
}
func DerefFloat64(p *float64) float64 {
if p != nil {
return *p
}
return DefaultFloat64
}
func Int64Slice(v []int64) *[]int64 {
return &v
}
func DerefInt64Slice(p *[]int64) []int64 {
if p != nil {
return *p
}
return make([]int64, 0)
}
func DerefStringSlice(p *[]string) []string {
if p != nil {
return *p
}
return make([]string, 0)
}
// 判断转为any的指针是否为空指针
func IsElemNil(elem any) bool {
efaceptr := (*eface)(unsafe.Pointer(&elem))
if efaceptr == nil || uintptr(efaceptr.ptr) == 0x0 {
return true
}
return false
}
// 判断转为any的切片是否为空切片
func IsSliceNil(elem any) bool {
efaceptr := (*eface)(unsafe.Pointer(&elem))
if efaceptr == nil || uintptr(efaceptr.ptr) == 0x0 {
return true
}
var sliceInfo = *(*[3]int)(efaceptr.ptr)
return sliceInfo[1] == 0
}