ptr

package
v2.679.0 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2026 License: MIT Imports: 0 Imported by: 0

Documentation

Overview

Package ptr provides small, generic helpers for working with pointers.

This package is intentionally tiny. It exists to reduce repetitive boilerplate when you need a pointer to a value (for example for optional configuration fields, APIs that take *T, or tests).

Helpers

Zero returns a pointer to the zero value of T:

var p *int = ptr.Zero[int]() // points to 0

Semantics and pitfalls

This helper allocates storage for the pointed-to zero value and returns a pointer to that storage. Each call returns a distinct pointer.

For example, two calls produce two different pointers even if the values are equal:

a := ptr.Zero[int]()
b := ptr.Zero[int]()
// a != b, but *a == *b

When not to use this package

If you already have an addressable variable, taking its address is typically clearer and avoids an extra helper call:

v := 42
p := &v

Similarly, for composite literals you can take the address directly:

p := &MyStruct{Field: "x"}

This package is best used when you need a pointer to a zero value in tests or configuration wiring.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Zero

func Zero[T any]() *T

Zero returns a pointer to the zero value of T.

This helper allocates storage for a zero value of T and returns a pointer to it. Each call returns a distinct pointer.

Types

This section is empty.

Jump to

Keyboard shortcuts

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