ptr

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2026 License: MIT Imports: 0 Imported by: 0

README

ptr

import "github.com/brpaz/lib-go/ptr"

Package ptr provides generic helper functions for working with pointer values.

Taking a pointer

Of returns a pointer to any value inline, avoiding the temporary-variable boilerplate required when an API demands a *T:

p := ptr.Of(42)       // *int pointing to 42
s := ptr.Of("hello")  // *string
Dereferencing safely

ValueOf dereferences a pointer and returns a fallback when the pointer is nil:

var p *int
n := ptr.ValueOf(p, 0)  // 0  — p is nil
p  = ptr.Of(7)
n  = ptr.ValueOf(p, 0)  // 7

Deref is the shorthand for the common case where the fallback is just T's zero value:

var p *int
n := ptr.Deref(p)  // 0  — p is nil
p  = ptr.Of(7)
n  = ptr.Deref(p)  // 7
Nil checks

IsNil is a readable nil guard:

if ptr.IsNil(cfg.Timeout) {
    // field was not set
}
Equality

Equal compares two pointers nil-safely by comparing the pointed-to values:

ptr.Equal(ptr.Of("foo"), ptr.Of("foo")) // true
ptr.Equal(nil, nil)                     // true  — both nil
ptr.Equal(ptr.Of("foo"), nil)           // false — one nil

Index

func Deref

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

Deref dereferences p and returns the value, or the zero value of T if p is nil.

func Equal

func Equal[T comparable](a, b *T) bool

Equal reports whether two pointers are equal. Both nil pointers are considered equal. Two non-nil pointers are equal when the values they point to are equal.

func IsNil

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

IsNil returns true if p is nil.

func Of

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

Of returns a pointer to the given value.

func ValueOf

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

ValueOf dereferences p and returns the value. If p is nil, fallback is returned instead.

Generated by gomarkdoc

Documentation

Overview

Package ptr provides generic helper functions for working with pointer values.

Taking a pointer

Of returns a pointer to any value inline, avoiding the temporary-variable boilerplate required when an API demands a *T:

p := ptr.Of(42)       // *int pointing to 42
s := ptr.Of("hello")  // *string

Dereferencing safely

ValueOf dereferences a pointer and returns a fallback when the pointer is nil:

var p *int
n := ptr.ValueOf(p, 0)  // 0  — p is nil
p  = ptr.Of(7)
n  = ptr.ValueOf(p, 0)  // 7

Deref is the shorthand for the common case where the fallback is just T's zero value:

var p *int
n := ptr.Deref(p)  // 0  — p is nil
p  = ptr.Of(7)
n  = ptr.Deref(p)  // 7

Nil checks

IsNil is a readable nil guard:

if ptr.IsNil(cfg.Timeout) {
    // field was not set
}

Equality

Equal compares two pointers nil-safely by comparing the pointed-to values:

ptr.Equal(ptr.Of("foo"), ptr.Of("foo")) // true
ptr.Equal(nil, nil)                     // true  — both nil
ptr.Equal(ptr.Of("foo"), nil)           // false — one nil

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Deref

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

Deref dereferences p and returns the value, or the zero value of T if p is nil.

func Equal

func Equal[T comparable](a, b *T) bool

Equal reports whether two pointers are equal. Both nil pointers are considered equal. Two non-nil pointers are equal when the values they point to are equal.

func IsNil

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

IsNil returns true if p is nil.

func Of

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

Of returns a pointer to the given value.

func ValueOf

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

ValueOf dereferences p and returns the value. If p is nil, fallback is returned instead.

Types

This section is empty.

Jump to

Keyboard shortcuts

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