pointer

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2021 License: MIT Imports: 1 Imported by: 331

README

pointer

Go Reference

Go package pointer provides helpers to convert between pointers and values of built-in (and, with Go 1.18+ generics, of any) types.

go get github.com/AlekSi/pointer

API is stable. Documentation.

package motivationalexample

import (
	"encoding/json"

	"github.com/AlekSi/pointer"
)

const (
	defaultName = "some name"
)

// Stuff contains optional fields.
type Stuff struct {
	Name    *string
	Comment *string
	Value   *int64
	Time    *time.Time
}

// SomeStuff makes some JSON-encoded stuff.
func SomeStuff() (data []byte, err error) {
	return json.Marshal(&Stuff{
		Name:    pointer.ToString(defaultName),                                   // can't say &defaultName
		Comment: pointer.ToString("not yet"),                                     // can't say &"not yet"
		Value:   pointer.ToInt64(42),                                             // can't say &42 or &int64(42)
		Time:    pointer.ToTime(time.Date(2014, 6, 25, 12, 24, 40, 0, time.UTC)), // can't say &time.Date(…)
	})
}

Documentation

Overview

Package pointer provides helpers to convert between pointers and values of built-in (and, with generics, of any) types.

Example
const (
	defaultName = "some name"
)

// Stuff contains optional fields.
type Stuff struct {
	Name    *string
	Comment *string
	Value   *int64
	Time    *time.Time
}

b, _ := json.Marshal(&Stuff{
	Name:    ToString(defaultName),                                   // can't say &defaultName
	Comment: ToString("not yet"),                                     // can't say &"not yet"
	Value:   ToInt64(42),                                             // can't say &42 or &int64(42)
	Time:    ToTime(time.Date(2014, 6, 25, 12, 24, 40, 0, time.UTC)), // can't say &time.Date(…)
})

fmt.Printf("%s", b)
Output:

{"Name":"some name","Comment":"not yet","Value":42,"Time":"2014-06-25T12:24:40Z"}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get added in v1.2.0

func Get[T any](t *T) T

Get returns the value from the passed pointer or the zero value if the pointer is nil.

func GetBool added in v1.1.0

func GetBool(b *bool) bool

GetBool returns the value of the bool pointer passed in or false if the pointer is nil.

func GetByte added in v1.1.0

func GetByte(b *byte) byte

GetByte returns the value of the byte pointer passed in or 0 if the pointer is nil.

func GetComplex128 added in v1.1.0

func GetComplex128(c *complex128) complex128

GetComplex128 returns the value of the complex128 pointer passed in or 0 if the pointer is nil.

func GetComplex64 added in v1.1.0

func GetComplex64(c *complex64) complex64

GetComplex64 returns the value of the complex64 pointer passed in or 0 if the pointer is nil.

func GetDuration added in v1.1.0

func GetDuration(d *time.Duration) time.Duration

GetDuration returns the value of the duration pointer passed in or 0 if the pointer is nil.

func GetError added in v1.1.0

func GetError(e *error) error

GetError returns the value of the error pointer passed in or nil if the pointer is nil.

func GetFloat32 added in v1.1.0

func GetFloat32(f *float32) float32

GetFloat32 returns the value of the float32 pointer passed in or 0 if the pointer is nil.

func GetFloat64 added in v1.1.0

func GetFloat64(f *float64) float64

GetFloat64 returns the value of the float64 pointer passed in or 0 if the pointer is nil.

func GetInt added in v1.1.0

func GetInt(i *int) int

GetInt returns the value of the int pointer passed in or 0 if the pointer is nil.

func GetInt16 added in v1.1.0

func GetInt16(i *int16) int16

GetInt16 returns the value of the int16 pointer passed in or 0 if the pointer is nil.

func GetInt32 added in v1.1.0

func GetInt32(i *int32) int32

GetInt32 returns the value of the int32 pointer passed in or 0 if the pointer is nil.

func GetInt64 added in v1.1.0

func GetInt64(i *int64) int64

GetInt64 returns the value of the int64 pointer passed in or 0 if the pointer is nil.

func GetInt8 added in v1.1.0

func GetInt8(i *int8) int8

GetInt8 returns the value of the int8 pointer passed in or 0 if the pointer is nil.

func GetRune added in v1.1.0

func GetRune(r *rune) rune

GetRune returns the value of the rune pointer passed in or 0 if the pointer is nil.

func GetString added in v1.1.0

func GetString(s *string) string

GetString returns the value of the string pointer passed in or empty string if the pointer is nil.

func GetTime added in v1.1.0

func GetTime(t *time.Time) time.Time

GetTime returns the value of the time pointer passed in or zero time.Time if the pointer is nil.

func GetUint added in v1.1.0

func GetUint(u *uint) uint

GetUint returns the value of the uint pointer passed in or 0 if the pointer is nil.

func GetUint16 added in v1.1.0

func GetUint16(u *uint16) uint16

GetUint16 returns the value of the uint16 pointer passed in or 0 if the pointer is nil.

func GetUint32 added in v1.1.0

func GetUint32(u *uint32) uint32

GetUint32 returns the value of the uint32 pointer passed in or 0 if the pointer is nil.

func GetUint64 added in v1.1.0

func GetUint64(u *uint64) uint64

GetUint64 returns the value of the uint64 pointer passed in or 0 if the pointer is nil.

func GetUint8 added in v1.1.0

func GetUint8(u *uint8) uint8

GetUint8 returns the value of the uint8 pointer passed in or 0 if the pointer is nil.

func GetUintptr added in v1.1.0

func GetUintptr(u *uintptr) uintptr

GetUintptr returns the value of the uintptr pointer passed in or 0 if the pointer is nil.

func To added in v1.2.0

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

To returns a pointer to the passed value.

func ToBool

func ToBool(b bool) *bool

ToBool returns a pointer to the passed bool value.

func ToBoolOrNil added in v1.1.0

func ToBoolOrNil(b bool) *bool

ToBoolOrNil returns a pointer to the passed bool value, or nil, if passed value is a zero value.

func ToByte

func ToByte(b byte) *byte

ToByte returns a pointer to the passed byte value.

func ToByteOrNil added in v1.1.0

func ToByteOrNil(b byte) *byte

ToByteOrNil returns a pointer to the passed byte value, or nil, if passed value is a zero value.

func ToComplex128

func ToComplex128(c complex128) *complex128

ToComplex128 returns a pointer to the passed complex128 value.

func ToComplex128OrNil added in v1.1.0

func ToComplex128OrNil(c complex128) *complex128

ToComplex128OrNil returns a pointer to the passed complex128 value, or nil, if passed value is a zero value.

func ToComplex64

func ToComplex64(c complex64) *complex64

ToComplex64 returns a pointer to the passed complex64 value.

func ToComplex64OrNil added in v1.1.0

func ToComplex64OrNil(c complex64) *complex64

ToComplex64OrNil returns a pointer to the passed complex64 value, or nil, if passed value is a zero value.

func ToDuration added in v1.1.0

func ToDuration(d time.Duration) *time.Duration

ToDuration returns a pointer to the passed time.Duration value.

func ToDurationOrNil added in v1.1.0

func ToDurationOrNil(d time.Duration) *time.Duration

ToDurationOrNil returns a pointer to the passed time.Duration value, or nil, if passed value is a zero value.

func ToError

func ToError(e error) *error

ToError returns a pointer to the passed error value.

func ToErrorOrNil added in v1.1.0

func ToErrorOrNil(e error) *error

ToErrorOrNil returns a pointer to the passed error value, or nil, if passed value is a zero value.

func ToFloat32

func ToFloat32(f float32) *float32

ToFloat32 returns a pointer to the passed float32 value.

func ToFloat32OrNil added in v1.1.0

func ToFloat32OrNil(f float32) *float32

ToFloat32OrNil returns a pointer to the passed float32 value, or nil, if passed value is a zero value.

func ToFloat64

func ToFloat64(f float64) *float64

ToFloat64 returns a pointer to the passed float64 value.

func ToFloat64OrNil added in v1.1.0

func ToFloat64OrNil(f float64) *float64

ToFloat64OrNil returns a pointer to the passed float64 value, or nil, if passed value is a zero value.

func ToInt

func ToInt(i int) *int

ToInt returns a pointer to the passed int value.

func ToInt16

func ToInt16(i int16) *int16

ToInt16 returns a pointer to the passed int16 value.

func ToInt16OrNil added in v1.1.0

func ToInt16OrNil(i int16) *int16

ToInt16OrNil returns a pointer to the passed int16 value, or nil, if passed value is a zero value.

func ToInt32

func ToInt32(i int32) *int32

ToInt32 returns a pointer to the passed int32 value.

func ToInt32OrNil added in v1.1.0

func ToInt32OrNil(i int32) *int32

ToInt32OrNil returns a pointer to the passed int32 value, or nil, if passed value is a zero value.

func ToInt64

func ToInt64(i int64) *int64

ToInt64 returns a pointer to the passed int64 value.

func ToInt64OrNil added in v1.1.0

func ToInt64OrNil(i int64) *int64

ToInt64OrNil returns a pointer to the passed int64 value, or nil, if passed value is a zero value.

func ToInt8

func ToInt8(i int8) *int8

ToInt8 returns a pointer to the passed int8 value.

func ToInt8OrNil added in v1.1.0

func ToInt8OrNil(i int8) *int8

ToInt8OrNil returns a pointer to the passed int8 value, or nil, if passed value is a zero value.

func ToIntOrNil added in v1.1.0

func ToIntOrNil(i int) *int

ToIntOrNil returns a pointer to the passed int value, or nil, if passed value is a zero value.

func ToOrNil added in v1.2.0

func ToOrNil[T comparable](t T) *T

ToOrNil returns a pointer to the passed value, or nil, if the passed value is a zero value. If the passed value has `IsZero() bool` method (for example, time.Time instance), it is used to determine if the value is zero.

func ToRune

func ToRune(r rune) *rune

ToRune returns a pointer to the passed rune value.

func ToRuneOrNil added in v1.1.0

func ToRuneOrNil(r rune) *rune

ToRuneOrNil returns a pointer to the passed rune value, or nil, if passed value is a zero value.

func ToString

func ToString(s string) *string

ToString returns a pointer to the passed string value.

func ToStringOrNil added in v1.1.0

func ToStringOrNil(s string) *string

ToStringOrNil returns a pointer to the passed string value, or nil, if passed value is a zero value.

func ToTime

func ToTime(t time.Time) *time.Time

ToTime returns a pointer to the passed time.Time value.

func ToTimeOrNil added in v1.1.0

func ToTimeOrNil(t time.Time) *time.Time

ToTimeOrNil returns a pointer to the passed time.Time value, or nil, if passed value is a zero value (t.IsZero() returns true).

func ToUint

func ToUint(u uint) *uint

ToUint returns a pointer to the passed uint value.

func ToUint16

func ToUint16(u uint16) *uint16

ToUint16 returns a pointer to the passed uint16 value.

func ToUint16OrNil added in v1.1.0

func ToUint16OrNil(u uint16) *uint16

ToUint16OrNil returns a pointer to the passed uint16 value, or nil, if passed value is a zero value.

func ToUint32

func ToUint32(u uint32) *uint32

ToUint32 returns a pointer to the passed uint32 value.

func ToUint32OrNil added in v1.1.0

func ToUint32OrNil(u uint32) *uint32

ToUint32OrNil returns a pointer to the passed uint32 value, or nil, if passed value is a zero value.

func ToUint64

func ToUint64(u uint64) *uint64

ToUint64 returns a pointer to the passed uint64 value.

func ToUint64OrNil added in v1.1.0

func ToUint64OrNil(u uint64) *uint64

ToUint64OrNil returns a pointer to the passed uint64 value, or nil, if passed value is a zero value.

func ToUint8

func ToUint8(u uint8) *uint8

ToUint8 returns a pointer to the passed uint8 value.

func ToUint8OrNil added in v1.1.0

func ToUint8OrNil(u uint8) *uint8

ToUint8OrNil returns a pointer to the passed uint8 value, or nil, if passed value is a zero value.

func ToUintOrNil added in v1.1.0

func ToUintOrNil(u uint) *uint

ToUintOrNil returns a pointer to the passed uint value, or nil, if passed value is a zero value.

func ToUintptr

func ToUintptr(u uintptr) *uintptr

ToUintptr returns a pointer to the passed uintptr value.

func ToUintptrOrNil added in v1.1.0

func ToUintptrOrNil(u uintptr) *uintptr

ToUintptrOrNil returns a pointer to the passed uintptr value, or nil, if passed value is a zero value.

Types

This section is empty.

Jump to

Keyboard shortcuts

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