mirror of
https://github.com/samber/lo.git
synced 2025-11-02 12:24:15 +08:00
@@ -1,10 +1,23 @@
|
||||
package lo
|
||||
|
||||
import "reflect"
|
||||
|
||||
// ToPtr returns a pointer copy of value.
|
||||
func ToPtr[T any](x T) *T {
|
||||
return &x
|
||||
}
|
||||
|
||||
// EmptyableToPtr returns a pointer copy of value if it's nonzero.
|
||||
// Otherwise, returns nil pointer.
|
||||
func EmptyableToPtr[T any](x T) *T {
|
||||
isZero := reflect.ValueOf(&x).Elem().IsZero()
|
||||
if isZero {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &x
|
||||
}
|
||||
|
||||
// FromPtr returns the pointer value or empty.
|
||||
func FromPtr[T any](x *T) T {
|
||||
if x == nil {
|
||||
|
||||
Reference in New Issue
Block a user