pointer

package
v2.3.2 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 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 IsNil added in v2.3.2

func IsNil(i interface{}) bool

IsNil returns true if the given interface is nil or the underlying value is nil.

Example
a := 1
b := &a
c := &b
d := &c
e := &d
var f *int

result1 := IsNil(a)
result2 := IsNil(b)
result3 := IsNil(c)
result4 := IsNil(d)
result5 := IsNil(e)
result6 := IsNil(f)
result7 := IsNil(nil)

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
fmt.Println(result5)
fmt.Println(result6)
fmt.Println(result7)
Output:

false
false
false
false
false
true
true

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
Deprecated: Please use UnwrapOr
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
Deprecated: Please use UnwrapOr
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
Deprecated: Please use UnwrapOr
Example
a := 123
b := "abc"

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

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

123
abc

func UnwrapOr added in v2.3.2

func UnwrapOr[T any](p *T, fallback ...T) T

UnwrapOr returns the value from the pointer or fallback if the pointer is nil.

Example
a := 123
b := "abc"

var c *int
var d *string

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

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

123
abc
456
def

Types

This section is empty.

Jump to

Keyboard shortcuts

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