Help us shape the future of Go! Take the 2026 Go Developer Survey

ptr

package
v0.6.4 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2026 License: MIT Imports: 0 Imported by: 0

README

ptr

import "github.com/gechr/x/ptr"

Package ptr provides pointer helpers.

Index

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
s := "hello"
fmt.Println(ptr.Deref(&s))

Output:

hello
Example (Nil)

A nil pointer dereferences to the zero value instead of panicking.

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.

type Config struct {
    Retries *int
}

retries := 3
fmt.Println(ptr.Deref(Config{Retries: &retries}.Retries))
fmt.Println(ptr.Deref(Config{}.Retries))

Output:

3
0

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.

Jump to

Keyboard shortcuts

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