Documentation
¶
Overview ¶
Package ptr provides pointer helpers.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Deref ¶
func Deref[T any](p *T) T
Deref returns the value `p` points to, or the zero value when `p` is nil.
Example ¶
package main
import (
"fmt"
"github.com/gechr/x/ptr"
)
func main() {
s := "hello"
fmt.Println(ptr.Deref(&s))
}
Output: hello
Example (Nil) ¶
A nil pointer dereferences to the zero value instead of panicking.
package main
import (
"fmt"
"github.com/gechr/x/ptr"
)
func main() {
var s *string
var n *int
fmt.Printf("%q\n", ptr.Deref(s))
fmt.Println(ptr.Deref(n))
}
Output: "" 0
Example (OptionalField) ¶
Deref is handy for optional struct fields modelled as pointers.
package main
import (
"fmt"
"github.com/gechr/x/ptr"
)
func main() {
type Config struct {
Retries *int
}
retries := 3
fmt.Println(ptr.Deref(Config{Retries: &retries}.Retries))
fmt.Println(ptr.Deref(Config{}.Retries))
}
Output: 3 0
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.