Documentation ¶
Index ¶
- func AllPtrFieldsNil(obj interface{}) bool
- func Clone[T any](p *T) *T
- func CloneBy[T any](p *T, f func(T) T) *T
- func Equal[T comparable](a, b *T) bool
- func EqualTo[T comparable](p *T, v T) bool
- func From[T any](v *T) T
- func FromOr[T any](ptr *T, def T) T
- func IsNil[T any](p *T) bool
- func IsNotNil[T any](p *T) bool
- func Map[F, T any](p *F, f func(F) T) *T
- func To[T any](v T) *T
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AllPtrFieldsNil ¶
func AllPtrFieldsNil(obj interface{}) bool
AllPtrFieldsNil tests whether all pointer fields in a struct are nil. This is useful when, for example, an API struct is handled by plugins which need to distinguish "no plugin accepted this spec" from "this spec is empty".
This function is only valid for structs and pointers to structs. Any other type will cause a panic. Passing a typed nil pointer will return true.
func Clone ¶
func Clone[T any](p *T) *T
Clone returns a shallow copy of the slice. If the given pointer is nil, nil is returned.
HINT: The element is copied using assignment (=), so this is a shallow clone. If you want to do a deep clone, use CloneBy with an appropriate element clone function.
AKA: Copy
func CloneBy ¶
func CloneBy[T any](p *T, f func(T) T) *T
CloneBy is variant of Clone, it returns a copy of the map. Element is copied using function f. If the given pointer is nil, nil is returned.
func Equal ¶
func Equal[T comparable](a, b *T) bool
Equal returns true if both arguments are nil or both arguments dereference to the same value.
func EqualTo ¶
func EqualTo[T comparable](p *T, v T) bool
EqualTo returns whether the value of pointer p is equal to value v. It a shortcut of "x != nil && *x == y".
EXAMPLE:
x, y := 1, 2 Equal(&x, 1) ⏩ true Equal(&y, 1) ⏩n false Equal(nil, 1) ⏩ false
func From ¶
func From[T any](v *T) T
From returns the value pointed to by the pointer p. If the pointer is nil, returns the zero value of T instead.
func FromOr ¶
func FromOr[T any](ptr *T, def T) T
FromOr dereferences ptr and returns the value it points to if no nil, or else returns def.
func Map ¶
func Map[F, T any](p *F, f func(F) T) *T
Map applies function f to element of pointer p. If p is nil, f will not be called and nil is returned, otherwise, result of f are returned as a new pointer.
EXAMPLE:
i := 1 Map(&i, strconv.Itoa) ⏩ (*string)("1") Map[int](nil, strconv.Itoa) ⏩ (*string)(nil)
Types ¶
This section is empty.