Documentation
¶
Overview ¶
Package ptr provides value to pointer and pointer to value functions.
In most cases, simply use ptr.To or ptr.From functions. There are some extra functions for types which would normally need type conversions from constants, such as ptr.ToInt64.
Package ptr provides utility functions for working with pointers in Go.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func EmptyToNil ¶
func EmptyToNil[T comparable](value *T) *T
EmptyToNil checks if the pointer is nil or if it points to a zero value of type T. If it is nil or points to a zero value, it returns nil. Otherwise, it returns a pointer to the value it points to.
func OrTo ¶
func OrTo[T any](value *T, defaultValue T) *T
OrTo returns the pointer if not nil, else return a pointer to the default value.
func ToNilIfEmpty ¶
func ToNilIfEmpty[T comparable](value T) *T
ToNilIfEmpty returns a pointer to the given value if it is not equal to the zero value of type T. If it is equal to the zero value, it returns nil.
func ValueOr ¶
func ValueOr[T any](value *T, defaultValue T) T
ValueOr return value of pointer if not nil, else return default value.
func ValueOrEmpty ¶
func ValueOrEmpty[T any](ref *T) (value T)
ValueOrEmpty returns the value from a given pointer. If ref is nil, a zero value of type T will be returned.
Types ¶
This section is empty.