# README
pointer
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(…)
})
}
# Functions
Get returns the value from the passed pointer or the zero value if the pointer is nil.
GetBool returns the value of the bool pointer passed in or false if the pointer is nil.
GetByte returns the value of the byte pointer passed in or 0 if the pointer is nil.
GetComplex128 returns the value of the complex128 pointer passed in or 0 if the pointer is nil.
GetComplex64 returns the value of the complex64 pointer passed in or 0 if the pointer is nil.
GetDuration returns the value of the duration pointer passed in or 0 if the pointer is nil.
GetError returns the value of the error pointer passed in or nil if the pointer is nil.
GetFloat32 returns the value of the float32 pointer passed in or 0 if the pointer is nil.
GetFloat64 returns the value of the float64 pointer passed in or 0 if the pointer is nil.
GetInt returns the value of the int pointer passed in or 0 if the pointer is nil.
GetInt16 returns the value of the int16 pointer passed in or 0 if the pointer is nil.
GetInt32 returns the value of the int32 pointer passed in or 0 if the pointer is nil.
GetInt64 returns the value of the int64 pointer passed in or 0 if the pointer is nil.
GetInt8 returns the value of the int8 pointer passed in or 0 if the pointer is nil.
GetRune returns the value of the rune pointer passed in or 0 if the pointer is nil.
GetString returns the value of the string pointer passed in or empty string if the pointer is nil.
GetTime returns the value of the time pointer passed in or zero time.Time if the pointer is nil.
GetUint returns the value of the uint pointer passed in or 0 if the pointer is nil.
GetUint16 returns the value of the uint16 pointer passed in or 0 if the pointer is nil.
GetUint32 returns the value of the uint32 pointer passed in or 0 if the pointer is nil.
GetUint64 returns the value of the uint64 pointer passed in or 0 if the pointer is nil.
GetUint8 returns the value of the uint8 pointer passed in or 0 if the pointer is nil.
GetUintptr returns the value of the uintptr pointer passed in or 0 if the pointer is nil.
To returns a pointer to the passed value.
ToBool returns a pointer to the passed bool value.
ToBoolOrNil returns a pointer to the passed bool value, or nil, if passed value is a zero value.
ToByte returns a pointer to the passed byte value.
ToByteOrNil returns a pointer to the passed byte value, or nil, if passed value is a zero value.
ToComplex128 returns a pointer to the passed complex128 value.
ToComplex128OrNil returns a pointer to the passed complex128 value, or nil, if passed value is a zero value.
ToComplex64 returns a pointer to the passed complex64 value.
ToComplex64OrNil returns a pointer to the passed complex64 value, or nil, if passed value is a zero value.
ToDuration returns a pointer to the passed time.Duration value.
ToDurationOrNil returns a pointer to the passed time.Duration value, or nil, if passed value is a zero value.
ToError returns a pointer to the passed error value.
ToErrorOrNil returns a pointer to the passed error value, or nil, if passed value is a zero value.
ToFloat32 returns a pointer to the passed float32 value.
ToFloat32OrNil returns a pointer to the passed float32 value, or nil, if passed value is a zero value.
ToFloat64 returns a pointer to the passed float64 value.
ToFloat64OrNil returns a pointer to the passed float64 value, or nil, if passed value is a zero value.
ToInt returns a pointer to the passed int value.
ToInt16 returns a pointer to the passed int16 value.
ToInt16OrNil returns a pointer to the passed int16 value, or nil, if passed value is a zero value.
ToInt32 returns a pointer to the passed int32 value.
ToInt32OrNil returns a pointer to the passed int32 value, or nil, if passed value is a zero value.
ToInt64 returns a pointer to the passed int64 value.
ToInt64OrNil returns a pointer to the passed int64 value, or nil, if passed value is a zero value.
ToInt8 returns a pointer to the passed int8 value.
ToInt8OrNil returns a pointer to the passed int8 value, or nil, if passed value is a zero value.
ToIntOrNil returns a pointer to the passed int value, or nil, if passed value is a zero value.
ToOrNil returns a pointer to the passed value, or nil, if the passed value is a zero value.
ToRune returns a pointer to the passed rune value.
ToRuneOrNil returns a pointer to the passed rune value, or nil, if passed value is a zero value.
ToString returns a pointer to the passed string value.
ToStringOrNil returns a pointer to the passed string value, or nil, if passed value is a zero value.
ToTime returns a pointer to the passed time.Time value.
ToTimeOrNil returns a pointer to the passed time.Time value, or nil, if passed value is a zero value (t.IsZero() returns true).
ToUint returns a pointer to the passed uint value.
ToUint16 returns a pointer to the passed uint16 value.
ToUint16OrNil returns a pointer to the passed uint16 value, or nil, if passed value is a zero value.
ToUint32 returns a pointer to the passed uint32 value.
ToUint32OrNil returns a pointer to the passed uint32 value, or nil, if passed value is a zero value.
ToUint64 returns a pointer to the passed uint64 value.
ToUint64OrNil returns a pointer to the passed uint64 value, or nil, if passed value is a zero value.
ToUint8 returns a pointer to the passed uint8 value.
ToUint8OrNil returns a pointer to the passed uint8 value, or nil, if passed value is a zero value.
ToUintOrNil returns a pointer to the passed uint value, or nil, if passed value is a zero value.
ToUintptr returns a pointer to the passed uintptr value.
ToUintptrOrNil returns a pointer to the passed uintptr value, or nil, if passed value is a zero value.