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