gptr

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2025 License: Apache-2.0 Imports: 2 Imported by: 56

Documentation

Overview

Package gptr provides generic operations for pointers.

💡 HINT: We provide similar functionality for different types in different packages. For example, github.com/bytedance/gg/gslice.Clone for copying slice while github.com/bytedance/gg/gmap.Clone for copying map.

Operations

Example
a := Of(1)
fmt.Println(Indirect(a)) // 1

b := OfNotZero(1)
fmt.Println(IsNotNil(b))                    // true
fmt.Println(IndirectOr(b, 2))               // 1
fmt.Println(Indirect(Map(b, strconv.Itoa))) // "1"

c := OfNotZero(0)
fmt.Println(c)                // nil
fmt.Println(IsNil(c))         // true
fmt.Println(IndirectOr(c, 2)) // 2
Output:

1
true
1
1
<nil>
true
2

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

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.

💡 AKA: CopyBy

func Equal

func Equal[T comparable](x, y *T) bool

Equal returns whether the given pointer x and y are equal.

Pointers x y are equal when either condition is satisfied:

  • Both x and y is nil (x == nil && y == nil)
  • x and y point to same address (x == y)
  • x and y point to same value (*x == *y)

🚀 EXAMPLE:

x, y, z := 1, 1, 2
Equal(&x, &x)          ⏩ true
Equal(&x, &y)          ⏩ true
Equal(&x, &z)          ⏩ false
Equal(&x, nil)         ⏩ false
Equal[int](nil, nil)   ⏩ true

💡 HINT: use EqualTo to compare between pointer and 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)   ⏩ false
Equal(nil, 1)  ⏩ false

func Indirect

func Indirect[T any](p *T) (v T)

Indirect returns the value pointed to by the pointer p. If the pointer is nil, returns the zero value of T instead.

🚀 EXAMPLE:

v := 1
var ptrV *int = &v
var ptrNil *int
Indirect(ptrV)    ⏩ 1
Indirect(ptrNil)  ⏩ 0

💡 HINT: Refer github.com/bytedance/gg/gvalue.Zero for definition of zero value.

💡 AKA: Unref, Unreference, Deref, Dereference

func IndirectOr

func IndirectOr[T any](p *T, fallback T) T

IndirectOr is a variant of Indirect, If the pointer is nil, returns the fallback value instead.

🚀 EXAMPLE:

v := 1
IndirectOr(&v, 100)   ⏩ 1
IndirectOr(nil, 100)  ⏩ 100

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 Of

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

Of returns a pointer that points to equivalent value of value v. (T → *T). It is useful when you want to "convert" a unaddressable value to pointer.

If you need to assign the address of a literal to a pointer:

 payload := struct {
	    Name *string
 }

The practice without generic:

x := "name"
payload.Name = &x

Use generic:

payload.Name = Of("name")

💡 HINT: use Indirect to dereference pointer (*T → T).

⚠️ WARNING: The returned pointer does not point to the original value because Go is always pass by value, user CAN NOT modify the value by modifying the pointer.

func OfNotZero

func OfNotZero[T comparable](v T) *T

OfNotZero is variant of Of, returns nil for zero value.

🚀 EXAMPLE:

OfNotZero(1)  ⏩ (*int)(1)
OfNotZero(0)  ⏩ (*int)(nil)

💡 HINT: Refer github.com/bytedance/gg/gvalue.Zero for definition of zero value.

func OfPositive

func OfPositive[T constraints.Number](v T) *T

OfPositive is variant of Of, returns nil for non-positive number.

🚀 EXAMPLE:

OfPositive(1)   ⏩ (*int)(1)
OfPositive(0)   ⏩ (*int)(nil)
OfPositive(-1)  ⏩ (*int)(nil)

Types

This section is empty.

Jump to

Keyboard shortcuts

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