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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.
Click to show internal directories.
Click to hide internal directories.