ptr

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2025 License: MIT Imports: 0 Imported by: 0

Documentation

Overview

Package ptr provides generic utility functions for working with pointers. It simplifies common operations such as creating a pointer from a literal value, safely dereferencing a potentially nil pointer, and comparing pointer values.

Creating Pointers with `Of`

In Go, you cannot take the address of a literal value directly. This package solves that problem, which is especially useful for populating struct fields that are pointers.

type Config struct {
	Timeout *int
	Name    *string
}

// Verbose way without the ptr package:
//
// timeout := 30
// name := "default-name"
// cfg := Config{
// 	Timeout: &timeout,
// 	Name:    &name,
// }

// Concise way with the ptr package:
cfg := Config{
	Timeout: ptr.Of(30),
	Name:    ptr.Of("default-name"),
}

Safely Dereferencing with `Value`

Dereferencing a nil pointer causes a panic. The `Value` function provides a safe way to get the value of a pointer, returning the zero value of the type if the pointer is nil.

var timeout *int // nil
var name = ptr.Of("my-app")

// Safely get the value or the zero value (0 for int).
timeoutValue := ptr.Value(timeout) // Returns 0

// Safely get the value of a non-nil pointer.
nameValue := ptr.Value(name) // Returns "my-app"

Comparing Pointers with `Equal`

The `Equal` function safely compares the values that two pointers point to. It handles nil pointers gracefully.

p1 := ptr.Of(100)
p2 := ptr.Of(100)
p3 := ptr.Of(200)
var p4 *int // nil

ptr.Equal(p1, p2) // true
ptr.Equal(p1, p3) // false
ptr.Equal(p1, p4) // false
ptr.Equal(p4, p4) // true

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 given value v. This is a convenient helper for creating a pointer to a literal or a variable in a single expression, often used for struct field initialization.

Example:

config := &MyConfig{ Timeout: ptr.Of(5*time.Second) }

func To

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

To converts an `any` value to a pointer of type *T. It handles three cases:

  1. If v is of type T, it returns a pointer to it (&v).
  2. If v is already of type *T, it returns v directly.
  3. Otherwise, it returns a new pointer to a zero value of T.

func ToVal

func ToVal[T any](v any) T

ToVal converts an `any` value to a value of type T. It handles three cases:

  1. If v is a non-nil pointer of type *T, it returns the dereferenced value.
  2. If v is of type T, it returns v directly.
  3. Otherwise, it returns the zero value of T.

func Val

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

Val returns the value that the pointer v points to. If the pointer is nil, it safely returns the zero value of the type T. This avoids a panic when dereferencing a nil pointer.

Types

This section is empty.

Jump to

Keyboard shortcuts

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