pointer

package
v2.3.1 Latest Latest
Warning

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

Go to latest
Published: May 14, 2024 License: MIT Imports: 1 Imported by: 5

Documentation

Overview

Package pointer contains some util functions to operate go pointer.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractPointer

func ExtractPointer(value any) any

ExtractPointer returns the underlying value by the given interface type Play: https://go.dev/play/p/D7HFjeWU2ZP

Example
a := 1
b := &a
c := &b
d := &c

result := ExtractPointer(d)

fmt.Println(result)
Output:

1

func Of added in v2.2.2

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

Of returns a pointer to the value `v`. Play: https://go.dev/play/p/HFd70x4DrMj

Example
result1 := Of(123)
result2 := Of("abc")

fmt.Println(*result1)
fmt.Println(*result2)
Output:

123
abc

func UnwarpOr added in v2.2.4

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

UnwarpOr returns the value from the pointer or fallback if the pointer is nil. Play: https://go.dev/play/p/mmNaLC38W8C

Example
a := 123
b := "abc"

var c *int
var d *string

result1 := UnwarpOr(&a, 456)
result2 := UnwarpOr(&b, "abc")
result3 := UnwarpOr(c, 456)
result4 := UnwarpOr(d, "def")

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
Output:

123
abc
456
def

func UnwarpOrDefault added in v2.2.4

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

UnwarpOrDefault returns the value from the pointer or the default value if the pointer is nil. Play: https://go.dev/play/p/ZnGIHf8_o4E

Example
a := 123
b := "abc"

var c *int
var d *string

result1 := UnwarpOrDefault(&a)
result2 := UnwarpOrDefault(&b)
result3 := UnwarpOrDefault(c)
result4 := UnwarpOrDefault(d)

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
Output:

123
abc
0

func Unwrap added in v2.2.2

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

Unwrap returns the value from the pointer. Play: https://go.dev/play/p/cgeu3g7cjWb

Example
a := 123
b := "abc"

result1 := Unwrap(&a)
result2 := Unwrap(&b)

fmt.Println(result1)
fmt.Println(result2)
Output:

123
abc

Types

This section is empty.

Jump to

Keyboard shortcuts

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