goext/langext/pointer.go

37 lines
507 B
Go
Raw Permalink Normal View History

2022-10-27 16:00:57 +02:00
package langext
import (
"reflect"
)
func Ptr[T any](v T) *T {
return &v
}
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.Array, reflect.Chan, reflect.Slice:
return reflect.ValueOf(i).IsNil()
}
return false
}