Documentation
¶
Overview ¶
Package pointer is a tiny, simple and obvious library to help deal safely with pointers
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Or ¶
func Or[T any](p *T, v T) T
Or returns the value p is pointing to if it is not nil, else v.
var s *string // nil pointer str := pointer.Or(s, "default") fmt.Println(str) // "default"
Example ¶
package main
import (
"fmt"
"github.com/FollowTheProcess/pointer"
)
func main() {
var s1 *string // nil pointer
s2 := "hello"
fmt.Printf("%q\n", pointer.Or(s1, "default"))
fmt.Printf("%q\n", pointer.Or(&s2, "you won't see me"))
}
Output: "default" "hello"
func OrDefault ¶
func OrDefault[T any](p *T) T
OrDefault returns the value p is pointing to if it is not nil, otherwise the zero value for the type T.
var s *string // nil pointer str := pointer.OrDefault(s) fmt.Println(str) // ""
Example ¶
package main
import (
"fmt"
"github.com/FollowTheProcess/pointer"
)
func main() {
var s1 *string // nil pointer
s2 := "wow!"
fmt.Printf("%q\n", pointer.OrDefault(s1))
fmt.Printf("%q\n", pointer.OrDefault(&s2))
}
Output: "" "wow!"
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.