goext/langext/pointer.go

64 lines
948 B
Go
Raw Normal View History

2022-10-27 16:00:57 +02:00
package langext
import (
"reflect"
)
2023-06-02 09:44:31 +02:00
// PTrue := &true
var PTrue = Ptr(true)
// PFalse := &false
var PFalse = Ptr(false)
2023-12-13 16:22:15 +01:00
// PNil := &nil
var PNil = Ptr[any](nil)
2022-10-27 16:00:57 +02:00
func Ptr[T any](v T) *T {
return &v
}
2023-12-13 16:22:15 +01:00
func DblPtr[T any](v T) **T {
v_ := &v
return &v_
}
func DblPtrNil[T any]() **T {
var v *T = nil
return &v
}
2024-04-25 11:47:16 +02:00
func ArrPtr[T any](v ...T) *[]T {
return &v
}
2022-10-27 16:00:57 +02:00
func PtrInt32(v int32) *int32 {
return &v
}
func PtrInt64(v int64) *int64 {
return &v
}
func PtrFloat32(v float32) *float32 {
return &v
}
func PtrFloat64(v float64) *float64 {
return &v
}
func IsNil(i interface{}) bool {
if i == nil {
return true
}
switch reflect.TypeOf(i).Kind() {
case reflect.Ptr, reflect.Map, reflect.Chan, reflect.Slice, reflect.Func, reflect.UnsafePointer:
2022-10-27 16:00:57 +02:00
return reflect.ValueOf(i).IsNil()
}
return false
}
2023-01-15 02:27:08 +01:00
func PtrEquals[T comparable](v1 *T, v2 *T) bool {
return (v1 == nil && v2 == nil) || (v1 != nil && v2 != nil && *v1 == *v2)
}