ptr

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 2, 2024 License: MIT Imports: 2 Imported by: 0

README

Pointer

This package provides some functions for pointer-based operations.

Documentation

Index

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 IsNil

func IsNil[T any](p *T) bool

IsNil returns whether the given pointer v is nil.

func IsNotNil

func IsNotNil[T any](p *T) bool

IsNotNil is negation of IsNil.

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)

func To

func To[T any](v T) *T

To returns a pointer to the given value.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL