ptr

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2023 License: Unlicense Imports: 0 Imported by: 0

README

ptr

Simple pointer helpers for Go

Go Reference

Obtaining pointer to the value

Simply pass the value to the To function, the type will be inferred by the Go compiler.

p := ptr.To(float32(0.5)) // p is *float32 pointing to the value 0.5

Retrieving value designated by pointer

To retrieve the value designated by pointer simply pass the pointer to From function. If the pointer might be nil you can supply optional default value as well. if the pointer is nil and default value isn't provided, From will panic.

// panics
_ = ptr.From((*float32)(nil))

var vv = float32(0.5)
value = ptr.From(&vv) // value = float32(0.5)
value = ptr.From((*float32)(nil), 0.7) // value = float32(0.7)

Documentation

Overview

Package ptr implements a simple pointer instrumentation. As it is based on Go generics, the minimal Go version is 1.18.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func From

func From[T any](pointer *T, dflt ...T) T

From will extract a value designated by pointer provider. It will panic if the pointer provided is nil and the default isn't supplied.

func To

func To[T any](value T) (pointer *T)

To will return a link to the value.

Types

This section is empty.

Jump to

Keyboard shortcuts

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