pointer

package
v13.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2026 License: AGPL-3.0 Imports: 0 Imported by: 0

Documentation

Overview

Package pointer provides generic utility functions for creating pointers to values and dereferencing pointer values and slices.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Dereference

func Dereference[T any](x *T) T

Dereference returns the value of a pointer.

Example
package main

import (
	"fmt"

	"github.com/primandproper/platform-go/v13/pointer"
)

func main() {
	s := "world"
	val := pointer.Dereference(&s)
	fmt.Println(val)
}
Output:
world
Example (Nil)
package main

import (
	"fmt"

	"github.com/primandproper/platform-go/v13/pointer"
)

func main() {
	val := pointer.Dereference[string](nil)
	fmt.Println(val)
}

func DereferenceSlice

func DereferenceSlice[T any](x []*T) []T

DereferenceSlice returns the value of a pointer for every element in a slice.

Example
package main

import (
	"fmt"

	"github.com/primandproper/platform-go/v13/pointer"
)

func main() {
	a, b, c := 10, 20, 30
	vals := pointer.DereferenceSlice([]*int{&a, &b, &c})
	fmt.Println(vals)
}
Output:
[10 20 30]

func To

func To[T any](x T) *T

To returns a pointer to a value.

It carries no //go:fix inline directive. The body is exactly what a caller would write by hand now that new takes a value, so one is tempting, but no tool can apply it: inlining a call to a generic function means inferring T, which the inliner does not do. All the directive produced was a diagnostic at every call site, and an exclusion in .golangci.yml for the directive itself.

Example
package main

import (
	"fmt"

	"github.com/primandproper/platform-go/v13/pointer"
)

func main() {
	p := pointer.To("hello")
	fmt.Println(*p)
}
Output:
hello

func ToSlice

func ToSlice[T any](x []T) []*T

ToSlice returns a pointer to each element in a slice.

Example
package main

import (
	"fmt"

	"github.com/primandproper/platform-go/v13/pointer"
)

func main() {
	vals := []int{1, 2, 3}
	ptrs := pointer.ToSlice(vals)
	fmt.Println(*ptrs[0], *ptrs[1], *ptrs[2])
}
Output:
1 2 3

Types

This section is empty.

Jump to

Keyboard shortcuts

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