ptr

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 19, 2026 License: MIT Imports: 0 Imported by: 0

Documentation

Overview

Package ptr provides generic utility functions for working with pointers.

This package simplifies common pointer operations using Go generics, particularly useful when working with optional values, protocol buffers, and database fields that may be nullable.

Creating Pointers

Use Of to create a pointer from a value:

age := ptr.Of(25)          // *int
name := ptr.Of("Alice")    // *string
active := ptr.Of(true)     // *bool

Create multiple pointers at once:

numbers := ptr.SliceOf(1, 2, 3)  // []*int

Dereferencing Pointers

Use To to safely dereference pointers with zero-value fallback:

var age *int = ptr.Of(25)
value := ptr.To(age)  // 25

var nilAge *int
value := ptr.To(nilAge)  // 0 (zero value for int)

Dereference slices of pointers:

ptrs := []*int{ptr.Of(1), nil, ptr.Of(3)}
values := ptr.SliceTo(ptrs...)  // []int{1, 0, 3}

Common Use Cases

Protocol buffer optional fields:

user := &pb.User{
    Name: ptr.Of("Alice"),
    Age:  ptr.Of(30),
}

Database nullable fields:

var deletedAt *time.Time
if shouldDelete {
    deletedAt = ptr.Of(time.Now())
}

Safe dereferencing:

displayAge := ptr.To(user.Age)  // Safe even if user.Age is nil

Type Safety

All functions use Go generics for type safety:

intPtr := ptr.Of(42)      // Type: *int
intVal := ptr.To(intPtr)  // Type: int

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Of

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

Of returns a pointer to the provided value.

This is useful when you need a pointer to a literal or value, such as for optional proto fields or nullable database columns.

Example:

age := ptr.Of(25)          // *int pointing to 25
name := ptr.Of("Alice")    // *string pointing to "Alice"
active := ptr.Of(true)     // *bool pointing to true

func SliceOf

func SliceOf[T any](vv ...T) []*T

SliceOf returns a slice of pointers from the specified values.

This is useful when you need to create a slice of optional values.

Example:

numbers := ptr.SliceOf(1, 2, 3)  // []*int{&1, &2, &3}
names := ptr.SliceOf("Alice", "Bob")  // []*string{&"Alice", &"Bob"}

func SliceTo

func SliceTo[T any](ps ...*T) []T

SliceTo returns a slice of values pointed to by the pointers.

Nil pointers in the input slice will be converted to zero values in the output slice.

Example:

ptrs := []*int{ptr.Of(1), nil, ptr.Of(3)}
values := ptr.SliceTo(ptrs...)  // []int{1, 0, 3}

func To

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

To returns the value pointed to by the pointer or its zero value if nil.

This provides safe dereferencing of pointers, returning the zero value for the type if the pointer is nil instead of panicking.

Example:

age := ptr.Of(25)
value := ptr.To(age)  // 25

var nilAge *int
value := ptr.To(nilAge)  // 0 (zero value for int)

Types

This section is empty.

Jump to

Keyboard shortcuts

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