Documentation
¶
Overview ¶
Package option provides a type-safe way to represent optional values in Go. A Generic[T] can either contain a value of type T (Some) or be empty (None).
This is useful for:
- Clearly representing nullable fields in structs.
- Avoiding nil pointer dereferences.
- Providing explicit intent about optional values.
Index ¶
- type Bool
- func (o *Bool) DecodeMsgpack(decoder *msgpack.Decoder) error
- func (o Bool) EncodeMsgpack(encoder *msgpack.Encoder) error
- func (o Bool) Get() (bool, bool)
- func (o Bool) IsNil() bool
- func (o Bool) IsSome() bool
- func (o Bool) IsZero() bool
- func (o Bool) MustGet() bool
- func (o Bool) Unwrap() bool
- func (o Bool) UnwrapOr(defaultValue bool) bool
- func (o Bool) UnwrapOrElse(defaultValue func() bool) bool
- type Byte
- func (o *Byte) DecodeMsgpack(decoder *msgpack.Decoder) error
- func (o Byte) EncodeMsgpack(encoder *msgpack.Encoder) error
- func (o Byte) Get() (byte, bool)
- func (o Byte) IsNil() bool
- func (o Byte) IsSome() bool
- func (o Byte) IsZero() bool
- func (o Byte) MustGet() byte
- func (o Byte) Unwrap() byte
- func (o Byte) UnwrapOr(defaultValue byte) byte
- func (o Byte) UnwrapOrElse(defaultValue func() byte) byte
- type Bytes
- func (o *Bytes) DecodeMsgpack(decoder *msgpack.Decoder) error
- func (o Bytes) EncodeMsgpack(encoder *msgpack.Encoder) error
- func (o Bytes) Get() ([]byte, bool)
- func (o Bytes) IsNil() bool
- func (o Bytes) IsSome() bool
- func (o Bytes) IsZero() bool
- func (o Bytes) MustGet() []byte
- func (o Bytes) Unwrap() []byte
- func (o Bytes) UnwrapOr(defaultValue []byte) []byte
- func (o Bytes) UnwrapOrElse(defaultValue func() []byte) []byte
- type DecodeError
- type EncodeError
- type Float32
- func (o *Float32) DecodeMsgpack(decoder *msgpack.Decoder) error
- func (o Float32) EncodeMsgpack(encoder *msgpack.Encoder) error
- func (o Float32) Get() (float32, bool)
- func (o Float32) IsNil() bool
- func (o Float32) IsSome() bool
- func (o Float32) IsZero() bool
- func (o Float32) MustGet() float32
- func (o Float32) Unwrap() float32
- func (o Float32) UnwrapOr(defaultValue float32) float32
- func (o Float32) UnwrapOrElse(defaultValue func() float32) float32
- type Float64
- func (o *Float64) DecodeMsgpack(decoder *msgpack.Decoder) error
- func (o Float64) EncodeMsgpack(encoder *msgpack.Encoder) error
- func (o Float64) Get() (float64, bool)
- func (o Float64) IsNil() bool
- func (o Float64) IsSome() bool
- func (o Float64) IsZero() bool
- func (o Float64) MustGet() float64
- func (o Float64) Unwrap() float64
- func (o Float64) UnwrapOr(defaultValue float64) float64
- func (o Float64) UnwrapOrElse(defaultValue func() float64) float64
- type Generic
- func (o *Generic[T]) DecodeMsgpack(decoder *msgpack.Decoder) error
- func (o Generic[T]) EncodeMsgpack(encoder *msgpack.Encoder) error
- func (o Generic[T]) Get() (T, bool)
- func (o Generic[T]) IsNil() bool
- func (o Generic[T]) IsSome() bool
- func (o Generic[T]) IsZero() bool
- func (o Generic[T]) MustGet() T
- func (o Generic[T]) Unwrap() T
- func (o Generic[T]) UnwrapOr(defaultValue T) T
- func (o Generic[T]) UnwrapOrElse(defaultValueFunc func() T) T
- type Int
- func (o *Int) DecodeMsgpack(decoder *msgpack.Decoder) error
- func (o Int) EncodeMsgpack(encoder *msgpack.Encoder) error
- func (o Int) Get() (int, bool)
- func (o Int) IsNil() bool
- func (o Int) IsSome() bool
- func (o Int) IsZero() bool
- func (o Int) MustGet() int
- func (o Int) Unwrap() int
- func (o Int) UnwrapOr(defaultValue int) int
- func (o Int) UnwrapOrElse(defaultValue func() int) int
- type Int16
- func (o *Int16) DecodeMsgpack(decoder *msgpack.Decoder) error
- func (o Int16) EncodeMsgpack(encoder *msgpack.Encoder) error
- func (o Int16) Get() (int16, bool)
- func (o Int16) IsNil() bool
- func (o Int16) IsSome() bool
- func (o Int16) IsZero() bool
- func (o Int16) MustGet() int16
- func (o Int16) Unwrap() int16
- func (o Int16) UnwrapOr(defaultValue int16) int16
- func (o Int16) UnwrapOrElse(defaultValue func() int16) int16
- type Int32
- func (o *Int32) DecodeMsgpack(decoder *msgpack.Decoder) error
- func (o Int32) EncodeMsgpack(encoder *msgpack.Encoder) error
- func (o Int32) Get() (int32, bool)
- func (o Int32) IsNil() bool
- func (o Int32) IsSome() bool
- func (o Int32) IsZero() bool
- func (o Int32) MustGet() int32
- func (o Int32) Unwrap() int32
- func (o Int32) UnwrapOr(defaultValue int32) int32
- func (o Int32) UnwrapOrElse(defaultValue func() int32) int32
- type Int64
- func (o *Int64) DecodeMsgpack(decoder *msgpack.Decoder) error
- func (o Int64) EncodeMsgpack(encoder *msgpack.Encoder) error
- func (o Int64) Get() (int64, bool)
- func (o Int64) IsNil() bool
- func (o Int64) IsSome() bool
- func (o Int64) IsZero() bool
- func (o Int64) MustGet() int64
- func (o Int64) Unwrap() int64
- func (o Int64) UnwrapOr(defaultValue int64) int64
- func (o Int64) UnwrapOrElse(defaultValue func() int64) int64
- type Int8
- func (o *Int8) DecodeMsgpack(decoder *msgpack.Decoder) error
- func (o Int8) EncodeMsgpack(encoder *msgpack.Encoder) error
- func (o Int8) Get() (int8, bool)
- func (o Int8) IsNil() bool
- func (o Int8) IsSome() bool
- func (o Int8) IsZero() bool
- func (o Int8) MustGet() int8
- func (o Int8) Unwrap() int8
- func (o Int8) UnwrapOr(defaultValue int8) int8
- func (o Int8) UnwrapOrElse(defaultValue func() int8) int8
- type String
- func (o *String) DecodeMsgpack(decoder *msgpack.Decoder) error
- func (o String) EncodeMsgpack(encoder *msgpack.Encoder) error
- func (o String) Get() (string, bool)
- func (o String) IsNil() bool
- func (o String) IsSome() bool
- func (o String) IsZero() bool
- func (o String) MustGet() string
- func (o String) Unwrap() string
- func (o String) UnwrapOr(defaultValue string) string
- func (o String) UnwrapOrElse(defaultValue func() string) string
- type Uint
- func (o *Uint) DecodeMsgpack(decoder *msgpack.Decoder) error
- func (o Uint) EncodeMsgpack(encoder *msgpack.Encoder) error
- func (o Uint) Get() (uint, bool)
- func (o Uint) IsNil() bool
- func (o Uint) IsSome() bool
- func (o Uint) IsZero() bool
- func (o Uint) MustGet() uint
- func (o Uint) Unwrap() uint
- func (o Uint) UnwrapOr(defaultValue uint) uint
- func (o Uint) UnwrapOrElse(defaultValue func() uint) uint
- type Uint16
- func (o *Uint16) DecodeMsgpack(decoder *msgpack.Decoder) error
- func (o Uint16) EncodeMsgpack(encoder *msgpack.Encoder) error
- func (o Uint16) Get() (uint16, bool)
- func (o Uint16) IsNil() bool
- func (o Uint16) IsSome() bool
- func (o Uint16) IsZero() bool
- func (o Uint16) MustGet() uint16
- func (o Uint16) Unwrap() uint16
- func (o Uint16) UnwrapOr(defaultValue uint16) uint16
- func (o Uint16) UnwrapOrElse(defaultValue func() uint16) uint16
- type Uint32
- func (o *Uint32) DecodeMsgpack(decoder *msgpack.Decoder) error
- func (o Uint32) EncodeMsgpack(encoder *msgpack.Encoder) error
- func (o Uint32) Get() (uint32, bool)
- func (o Uint32) IsNil() bool
- func (o Uint32) IsSome() bool
- func (o Uint32) IsZero() bool
- func (o Uint32) MustGet() uint32
- func (o Uint32) Unwrap() uint32
- func (o Uint32) UnwrapOr(defaultValue uint32) uint32
- func (o Uint32) UnwrapOrElse(defaultValue func() uint32) uint32
- type Uint64
- func (o *Uint64) DecodeMsgpack(decoder *msgpack.Decoder) error
- func (o Uint64) EncodeMsgpack(encoder *msgpack.Encoder) error
- func (o Uint64) Get() (uint64, bool)
- func (o Uint64) IsNil() bool
- func (o Uint64) IsSome() bool
- func (o Uint64) IsZero() bool
- func (o Uint64) MustGet() uint64
- func (o Uint64) Unwrap() uint64
- func (o Uint64) UnwrapOr(defaultValue uint64) uint64
- func (o Uint64) UnwrapOrElse(defaultValue func() uint64) uint64
- type Uint8
- func (o *Uint8) DecodeMsgpack(decoder *msgpack.Decoder) error
- func (o Uint8) EncodeMsgpack(encoder *msgpack.Encoder) error
- func (o Uint8) Get() (uint8, bool)
- func (o Uint8) IsNil() bool
- func (o Uint8) IsSome() bool
- func (o Uint8) IsZero() bool
- func (o Uint8) MustGet() uint8
- func (o Uint8) Unwrap() uint8
- func (o Uint8) UnwrapOr(defaultValue uint8) uint8
- func (o Uint8) UnwrapOrElse(defaultValue func() uint8) uint8
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Bool ¶
type Bool struct {
// contains filtered or unexported fields
}
Bool represents an optional value of type bool. It can either hold a valid bool (IsSome == true) or be empty (IsZero == true).
func NoneBool ¶
func NoneBool() Bool
NoneBool creates an empty optional Bool value. The returned Bool will have IsSome() == false and IsZero() == true.
Example:
o := NoneBool() if o.IsZero() { fmt.Println("value is absent") }
func SomeBool ¶
SomeBool creates an optional Bool with the given bool value. The returned Bool will have IsSome() == true and IsZero() == false.
Example:
o := SomeBool(true) if o.IsSome() { v := o.Unwrap() // v == true }
func (*Bool) DecodeMsgpack ¶
DecodeMsgpack decodes a Bool value from MessagePack format. Supports two input types:
- nil: interpreted as no value (NoneBool)
- bool: interpreted as a present value (SomeBool)
Returns an error if the input type is unsupported or decoding fails.
After successful decoding:
- on nil: exists = false, value = default zero value
- on bool: exists = true, value = decoded value
func (Bool) EncodeMsgpack ¶
EncodeMsgpack encodes the Bool value using MessagePack format. - If the value is present, it is encoded as bool. - If the value is absent (None), it is encoded as nil.
Returns an error if encoding fails.
func (Bool) Get ¶
Get returns the stored value and a boolean flag indicating its presence. If the value is present, returns (value, true). If the value is absent, returns (zero value of bool, false).
Recommended usage:
if value, ok := o.Get(); ok { // use value }
func (Bool) IsNil ¶
IsNil is an alias for IsZero.
This method is provided for compatibility with the msgpack Encoder interface.
func (Bool) IsSome ¶
IsSome returns true if the Bool contains a value. This indicates the value is explicitly set (not None).
func (Bool) IsZero ¶
IsZero returns true if the Bool does not contain a value. Equivalent to !IsSome(). Useful for consistency with types where zero value (e.g. 0, false, zero struct) is valid and needs to be distinguished.
func (Bool) MustGet ¶
MustGet returns the stored value if it is present. Panics if the value is absent (i.e., IsZero() == true).
Use with caution — only when you are certain the value exists.
Panics with: "optional value is not set" if no value is set.
func (Bool) Unwrap ¶
Unwrap returns the stored value regardless of presence. If no value is set, returns the zero value for bool.
Warning: Does not check presence. Use IsSome() before calling if you need to distinguish between absent value and explicit zero value.
func (Bool) UnwrapOr ¶
UnwrapOr returns the stored value if present. Otherwise, returns the provided default value.
Example:
o := NoneBool() v := o.UnwrapOr(someDefaultBool)
func (Bool) UnwrapOrElse ¶
UnwrapOrElse returns the stored value if present. Otherwise, calls the provided function and returns its result. Useful when the default value requires computation or side effects.
Example:
o := NoneBool() v := o.UnwrapOrElse(func() bool { return computeDefault() })
type Byte ¶
type Byte struct {
// contains filtered or unexported fields
}
Byte represents an optional value of type byte. It can either hold a valid byte (IsSome == true) or be empty (IsZero == true).
func NoneByte ¶
func NoneByte() Byte
NoneByte creates an empty optional Byte value. The returned Byte will have IsSome() == false and IsZero() == true.
Example:
o := NoneByte() if o.IsZero() { fmt.Println("value is absent") }
func SomeByte ¶
SomeByte creates an optional Byte with the given byte value. The returned Byte will have IsSome() == true and IsZero() == false.
Example:
o := SomeByte(12) if o.IsSome() { v := o.Unwrap() // v == 12 }
func (*Byte) DecodeMsgpack ¶
DecodeMsgpack decodes a Byte value from MessagePack format. Supports two input types:
- nil: interpreted as no value (NoneByte)
- byte: interpreted as a present value (SomeByte)
Returns an error if the input type is unsupported or decoding fails.
After successful decoding:
- on nil: exists = false, value = default zero value
- on byte: exists = true, value = decoded value
func (Byte) EncodeMsgpack ¶
EncodeMsgpack encodes the Byte value using MessagePack format. - If the value is present, it is encoded as byte. - If the value is absent (None), it is encoded as nil.
Returns an error if encoding fails.
func (Byte) Get ¶
Get returns the stored value and a boolean flag indicating its presence. If the value is present, returns (value, true). If the value is absent, returns (zero value of byte, false).
Recommended usage:
if value, ok := o.Get(); ok { // use value }
func (Byte) IsNil ¶
IsNil is an alias for IsZero.
This method is provided for compatibility with the msgpack Encoder interface.
func (Byte) IsSome ¶
IsSome returns true if the Byte contains a value. This indicates the value is explicitly set (not None).
func (Byte) IsZero ¶
IsZero returns true if the Byte does not contain a value. Equivalent to !IsSome(). Useful for consistency with types where zero value (e.g. 0, false, zero struct) is valid and needs to be distinguished.
func (Byte) MustGet ¶
MustGet returns the stored value if it is present. Panics if the value is absent (i.e., IsZero() == true).
Use with caution — only when you are certain the value exists.
Panics with: "optional value is not set" if no value is set.
func (Byte) Unwrap ¶
Unwrap returns the stored value regardless of presence. If no value is set, returns the zero value for byte.
Warning: Does not check presence. Use IsSome() before calling if you need to distinguish between absent value and explicit zero value.
func (Byte) UnwrapOr ¶
UnwrapOr returns the stored value if present. Otherwise, returns the provided default value.
Example:
o := NoneByte() v := o.UnwrapOr(someDefaultByte)
func (Byte) UnwrapOrElse ¶
UnwrapOrElse returns the stored value if present. Otherwise, calls the provided function and returns its result. Useful when the default value requires computation or side effects.
Example:
o := NoneByte() v := o.UnwrapOrElse(func() byte { return computeDefault() })
type Bytes ¶
type Bytes struct {
// contains filtered or unexported fields
}
Bytes represents an optional value of type []byte. It can either hold a valid []byte (IsSome == true) or be empty (IsZero == true).
func NoneBytes ¶
func NoneBytes() Bytes
NoneBytes creates an empty optional Bytes value. The returned Bytes will have IsSome() == false and IsZero() == true.
Example:
o := NoneBytes() if o.IsZero() { fmt.Println("value is absent") }
func SomeBytes ¶
SomeBytes creates an optional Bytes with the given []byte value. The returned Bytes will have IsSome() == true and IsZero() == false.
Example:
o := SomeBytes([]byte("hello")) if o.IsSome() { v := o.Unwrap() // v == []byte("hello") }
func (*Bytes) DecodeMsgpack ¶
DecodeMsgpack decodes a Bytes value from MessagePack format. Supports two input types:
- nil: interpreted as no value (NoneBytes)
- []byte: interpreted as a present value (SomeBytes)
Returns an error if the input type is unsupported or decoding fails.
After successful decoding:
- on nil: exists = false, value = default zero value
- on []byte: exists = true, value = decoded value
func (Bytes) EncodeMsgpack ¶
EncodeMsgpack encodes the Bytes value using MessagePack format. - If the value is present, it is encoded as []byte. - If the value is absent (None), it is encoded as nil.
Returns an error if encoding fails.
func (Bytes) Get ¶
Get returns the stored value and a boolean flag indicating its presence. If the value is present, returns (value, true). If the value is absent, returns (zero value of []byte, false).
Recommended usage:
if value, ok := o.Get(); ok { // use value }
func (Bytes) IsNil ¶
IsNil is an alias for IsZero.
This method is provided for compatibility with the msgpack Encoder interface.
func (Bytes) IsSome ¶
IsSome returns true if the Bytes contains a value. This indicates the value is explicitly set (not None).
func (Bytes) IsZero ¶
IsZero returns true if the Bytes does not contain a value. Equivalent to !IsSome(). Useful for consistency with types where zero value (e.g. 0, false, zero struct) is valid and needs to be distinguished.
func (Bytes) MustGet ¶
MustGet returns the stored value if it is present. Panics if the value is absent (i.e., IsZero() == true).
Use with caution — only when you are certain the value exists.
Panics with: "optional value is not set" if no value is set.
func (Bytes) Unwrap ¶
Unwrap returns the stored value regardless of presence. If no value is set, returns the zero value for []byte.
Warning: Does not check presence. Use IsSome() before calling if you need to distinguish between absent value and explicit zero value.
func (Bytes) UnwrapOr ¶
UnwrapOr returns the stored value if present. Otherwise, returns the provided default value.
Example:
o := NoneBytes() v := o.UnwrapOr(someDefaultBytes)
func (Bytes) UnwrapOrElse ¶
UnwrapOrElse returns the stored value if present. Otherwise, calls the provided function and returns its result. Useful when the default value requires computation or side effects.
Example:
o := NoneBytes() v := o.UnwrapOrElse(func() []byte { return computeDefault() })
type DecodeError ¶
DecodeError is returned when decoding failed due to invalid code in msgpack stream.
func (DecodeError) Error ¶
func (d DecodeError) Error() string
Error returns the text representation of error.
type EncodeError ¶
EncodeError is returned when encoding failed due to stream errors.
func (EncodeError) Error ¶
func (e EncodeError) Error() string
Error returns the text representation of error.
type Float32 ¶
type Float32 struct {
// contains filtered or unexported fields
}
Float32 represents an optional value of type float32. It can either hold a valid float32 (IsSome == true) or be empty (IsZero == true).
func NoneFloat32 ¶
func NoneFloat32() Float32
NoneFloat32 creates an empty optional Float32 value. The returned Float32 will have IsSome() == false and IsZero() == true.
Example:
o := NoneFloat32() if o.IsZero() { fmt.Println("value is absent") }
func SomeFloat32 ¶
SomeFloat32 creates an optional Float32 with the given float32 value. The returned Float32 will have IsSome() == true and IsZero() == false.
Example:
o := SomeFloat32(12) if o.IsSome() { v := o.Unwrap() // v == 12 }
func (*Float32) DecodeMsgpack ¶
DecodeMsgpack decodes a Float32 value from MessagePack format. Supports two input types:
- nil: interpreted as no value (NoneFloat32)
- float32: interpreted as a present value (SomeFloat32)
Returns an error if the input type is unsupported or decoding fails.
After successful decoding:
- on nil: exists = false, value = default zero value
- on float32: exists = true, value = decoded value
func (Float32) EncodeMsgpack ¶
EncodeMsgpack encodes the Float32 value using MessagePack format. - If the value is present, it is encoded as float32. - If the value is absent (None), it is encoded as nil.
Returns an error if encoding fails.
func (Float32) Get ¶
Get returns the stored value and a boolean flag indicating its presence. If the value is present, returns (value, true). If the value is absent, returns (zero value of float32, false).
Recommended usage:
if value, ok := o.Get(); ok { // use value }
func (Float32) IsNil ¶
IsNil is an alias for IsZero.
This method is provided for compatibility with the msgpack Encoder interface.
func (Float32) IsSome ¶
IsSome returns true if the Float32 contains a value. This indicates the value is explicitly set (not None).
func (Float32) IsZero ¶
IsZero returns true if the Float32 does not contain a value. Equivalent to !IsSome(). Useful for consistency with types where zero value (e.g. 0, false, zero struct) is valid and needs to be distinguished.
func (Float32) MustGet ¶
MustGet returns the stored value if it is present. Panics if the value is absent (i.e., IsZero() == true).
Use with caution — only when you are certain the value exists.
Panics with: "optional value is not set" if no value is set.
func (Float32) Unwrap ¶
Unwrap returns the stored value regardless of presence. If no value is set, returns the zero value for float32.
Warning: Does not check presence. Use IsSome() before calling if you need to distinguish between absent value and explicit zero value.
func (Float32) UnwrapOr ¶
UnwrapOr returns the stored value if present. Otherwise, returns the provided default value.
Example:
o := NoneFloat32() v := o.UnwrapOr(someDefaultFloat32)
func (Float32) UnwrapOrElse ¶
UnwrapOrElse returns the stored value if present. Otherwise, calls the provided function and returns its result. Useful when the default value requires computation or side effects.
Example:
o := NoneFloat32() v := o.UnwrapOrElse(func() float32 { return computeDefault() })
type Float64 ¶
type Float64 struct {
// contains filtered or unexported fields
}
Float64 represents an optional value of type float64. It can either hold a valid float64 (IsSome == true) or be empty (IsZero == true).
func NoneFloat64 ¶
func NoneFloat64() Float64
NoneFloat64 creates an empty optional Float64 value. The returned Float64 will have IsSome() == false and IsZero() == true.
Example:
o := NoneFloat64() if o.IsZero() { fmt.Println("value is absent") }
func SomeFloat64 ¶
SomeFloat64 creates an optional Float64 with the given float64 value. The returned Float64 will have IsSome() == true and IsZero() == false.
Example:
o := SomeFloat64(12) if o.IsSome() { v := o.Unwrap() // v == 12 }
func (*Float64) DecodeMsgpack ¶
DecodeMsgpack decodes a Float64 value from MessagePack format. Supports two input types:
- nil: interpreted as no value (NoneFloat64)
- float64: interpreted as a present value (SomeFloat64)
Returns an error if the input type is unsupported or decoding fails.
After successful decoding:
- on nil: exists = false, value = default zero value
- on float64: exists = true, value = decoded value
func (Float64) EncodeMsgpack ¶
EncodeMsgpack encodes the Float64 value using MessagePack format. - If the value is present, it is encoded as float64. - If the value is absent (None), it is encoded as nil.
Returns an error if encoding fails.
func (Float64) Get ¶
Get returns the stored value and a boolean flag indicating its presence. If the value is present, returns (value, true). If the value is absent, returns (zero value of float64, false).
Recommended usage:
if value, ok := o.Get(); ok { // use value }
func (Float64) IsNil ¶
IsNil is an alias for IsZero.
This method is provided for compatibility with the msgpack Encoder interface.
func (Float64) IsSome ¶
IsSome returns true if the Float64 contains a value. This indicates the value is explicitly set (not None).
func (Float64) IsZero ¶
IsZero returns true if the Float64 does not contain a value. Equivalent to !IsSome(). Useful for consistency with types where zero value (e.g. 0, false, zero struct) is valid and needs to be distinguished.
func (Float64) MustGet ¶
MustGet returns the stored value if it is present. Panics if the value is absent (i.e., IsZero() == true).
Use with caution — only when you are certain the value exists.
Panics with: "optional value is not set" if no value is set.
func (Float64) Unwrap ¶
Unwrap returns the stored value regardless of presence. If no value is set, returns the zero value for float64.
Warning: Does not check presence. Use IsSome() before calling if you need to distinguish between absent value and explicit zero value.
func (Float64) UnwrapOr ¶
UnwrapOr returns the stored value if present. Otherwise, returns the provided default value.
Example:
o := NoneFloat64() v := o.UnwrapOr(someDefaultFloat64)
func (Float64) UnwrapOrElse ¶
UnwrapOrElse returns the stored value if present. Otherwise, calls the provided function and returns its result. Useful when the default value requires computation or side effects.
Example:
o := NoneFloat64() v := o.UnwrapOrElse(func() float64 { return computeDefault() })
type Generic ¶
type Generic[T any] struct { // contains filtered or unexported fields }
Generic represents an optional value: it may contain a value of type T (Some), or it may be empty (None).
This type is useful for safely handling potentially absent values without relying on nil pointers or sentinel values, and avoids panics when proper checks are used.
Example:
opt := option.Some(42) if opt.IsSome() { fmt.Println(opt.Unwrap()) // prints 42 } var empty option.Generic[string] fmt.Println(empty.IsZero()) // true
func None ¶
None creates an Generic[T] that does not contain a value.
The returned Generic is in the "none" state, meaning IsZero() will return true.
Example:
opt := option.None[int]() fmt.Println(opt.IsZero()) // true
func Some ¶
Some creates a Generic[T] containing the given value.
The returned Generic is in the "some" state, meaning IsSome() will return true.
Example:
opt := option.Some("hello") fmt.Println(opt.IsSome()) // true
func (*Generic[T]) DecodeMsgpack ¶
DecodeMsgpack implements the msgpack.CustomDecoder interface.
It reads a MessagePack value and decodes it into the Generic.
- If the encoded value is nil, the optional is set to None.
- Otherwise, it decodes into the internal value, using a custom decoder if available, and marks the optional as Some.
Note: This method modifies the receiver and must be called on a pointer.
func (Generic[T]) EncodeMsgpack ¶
EncodeMsgpack implements the msgpack.CustomEncoder interface.
If the optional is empty (None), it encodes as a MessagePack nil. If the optional contains a value (Some), it attempts to use a custom encoder if the value implements msgpack.CustomEncoder; otherwise, it uses the standard encoder.
func (Generic[T]) Get ¶
Get returns the contained value and a boolean indicating whether the value exists.
This is the safest way to extract the value. The second return value is true if a value is present, false otherwise. The first return value is the zero value of T when no value exists.
Example:
value, ok := opt.Get() if ok { process(value) } else { fmt.Println("no value available") }
func (Generic[T]) IsNil ¶
IsNil is an alias for IsZero.
This method is provided for compatibility with the msgpack Encoder interface.
func (Generic[T]) IsSome ¶
IsSome returns true if the optional contains a value.
Example:
if opt.IsSome() { // safely access value }
func (Generic[T]) IsZero ¶
IsZero returns true if the optional does not contain a value.
Example:
if opt.IsZero() { log.Println("value is missing") }
func (Generic[T]) MustGet ¶
func (o Generic[T]) MustGet() T
MustGet returns the contained value if present.
Panics if the optional is in the "none" state (i.e., no value is present).
Only use this method when you are certain the value exists. For safer access, use Get() instead.
Example:
value := opt.MustGet() // panics if value not set
func (Generic[T]) Unwrap ¶
func (o Generic[T]) Unwrap() T
Unwrap returns the stored value regardless of presence. If no value is set, returns the zero value for T.
Warning: Does not check presence. Use IsSome() before calling if you need to distinguish between absent value and explicit zero value.
func (Generic[T]) UnwrapOr ¶
func (o Generic[T]) UnwrapOr(defaultValue T) T
UnwrapOr returns the contained value if present, otherwise returns the provided default value.
This is useful when you want to provide a simple fallback value.
Example:
name := opt.UnwrapOr("default")
func (Generic[T]) UnwrapOrElse ¶
func (o Generic[T]) UnwrapOrElse(defaultValueFunc func() T) T
UnwrapOrElse returns the contained value if present, otherwise calls the provided function to compute a default value.
This is useful when the default value is expensive to compute, or requires dynamic logic.
Example:
value := opt.UnwrapOrElse(fetchFromDatabase)
type Int ¶
type Int struct {
// contains filtered or unexported fields
}
Int represents an optional value of type int. It can either hold a valid int (IsSome == true) or be empty (IsZero == true).
func NoneInt ¶
func NoneInt() Int
NoneInt creates an empty optional Int value. The returned Int will have IsSome() == false and IsZero() == true.
Example:
o := NoneInt() if o.IsZero() { fmt.Println("value is absent") }
func SomeInt ¶
SomeInt creates an optional Int with the given int value. The returned Int will have IsSome() == true and IsZero() == false.
Example:
o := SomeInt(12) if o.IsSome() { v := o.Unwrap() // v == 12 }
func (*Int) DecodeMsgpack ¶
DecodeMsgpack decodes a Int value from MessagePack format. Supports two input types:
- nil: interpreted as no value (NoneInt)
- int: interpreted as a present value (SomeInt)
Returns an error if the input type is unsupported or decoding fails.
After successful decoding:
- on nil: exists = false, value = default zero value
- on int: exists = true, value = decoded value
func (Int) EncodeMsgpack ¶
EncodeMsgpack encodes the Int value using MessagePack format. - If the value is present, it is encoded as int. - If the value is absent (None), it is encoded as nil.
Returns an error if encoding fails.
func (Int) Get ¶
Get returns the stored value and a boolean flag indicating its presence. If the value is present, returns (value, true). If the value is absent, returns (zero value of int, false).
Recommended usage:
if value, ok := o.Get(); ok { // use value }
func (Int) IsNil ¶
IsNil is an alias for IsZero.
This method is provided for compatibility with the msgpack Encoder interface.
func (Int) IsSome ¶
IsSome returns true if the Int contains a value. This indicates the value is explicitly set (not None).
func (Int) IsZero ¶
IsZero returns true if the Int does not contain a value. Equivalent to !IsSome(). Useful for consistency with types where zero value (e.g. 0, false, zero struct) is valid and needs to be distinguished.
func (Int) MustGet ¶
MustGet returns the stored value if it is present. Panics if the value is absent (i.e., IsZero() == true).
Use with caution — only when you are certain the value exists.
Panics with: "optional value is not set" if no value is set.
func (Int) Unwrap ¶
Unwrap returns the stored value regardless of presence. If no value is set, returns the zero value for int.
Warning: Does not check presence. Use IsSome() before calling if you need to distinguish between absent value and explicit zero value.
func (Int) UnwrapOr ¶
UnwrapOr returns the stored value if present. Otherwise, returns the provided default value.
Example:
o := NoneInt() v := o.UnwrapOr(someDefaultInt)
func (Int) UnwrapOrElse ¶
UnwrapOrElse returns the stored value if present. Otherwise, calls the provided function and returns its result. Useful when the default value requires computation or side effects.
Example:
o := NoneInt() v := o.UnwrapOrElse(func() int { return computeDefault() })
type Int16 ¶
type Int16 struct {
// contains filtered or unexported fields
}
Int16 represents an optional value of type int16. It can either hold a valid int16 (IsSome == true) or be empty (IsZero == true).
func NoneInt16 ¶
func NoneInt16() Int16
NoneInt16 creates an empty optional Int16 value. The returned Int16 will have IsSome() == false and IsZero() == true.
Example:
o := NoneInt16() if o.IsZero() { fmt.Println("value is absent") }
func SomeInt16 ¶
SomeInt16 creates an optional Int16 with the given int16 value. The returned Int16 will have IsSome() == true and IsZero() == false.
Example:
o := SomeInt16(12) if o.IsSome() { v := o.Unwrap() // v == 12 }
func (*Int16) DecodeMsgpack ¶
DecodeMsgpack decodes a Int16 value from MessagePack format. Supports two input types:
- nil: interpreted as no value (NoneInt16)
- int16: interpreted as a present value (SomeInt16)
Returns an error if the input type is unsupported or decoding fails.
After successful decoding:
- on nil: exists = false, value = default zero value
- on int16: exists = true, value = decoded value
func (Int16) EncodeMsgpack ¶
EncodeMsgpack encodes the Int16 value using MessagePack format. - If the value is present, it is encoded as int16. - If the value is absent (None), it is encoded as nil.
Returns an error if encoding fails.
func (Int16) Get ¶
Get returns the stored value and a boolean flag indicating its presence. If the value is present, returns (value, true). If the value is absent, returns (zero value of int16, false).
Recommended usage:
if value, ok := o.Get(); ok { // use value }
func (Int16) IsNil ¶
IsNil is an alias for IsZero.
This method is provided for compatibility with the msgpack Encoder interface.
func (Int16) IsSome ¶
IsSome returns true if the Int16 contains a value. This indicates the value is explicitly set (not None).
func (Int16) IsZero ¶
IsZero returns true if the Int16 does not contain a value. Equivalent to !IsSome(). Useful for consistency with types where zero value (e.g. 0, false, zero struct) is valid and needs to be distinguished.
func (Int16) MustGet ¶
MustGet returns the stored value if it is present. Panics if the value is absent (i.e., IsZero() == true).
Use with caution — only when you are certain the value exists.
Panics with: "optional value is not set" if no value is set.
func (Int16) Unwrap ¶
Unwrap returns the stored value regardless of presence. If no value is set, returns the zero value for int16.
Warning: Does not check presence. Use IsSome() before calling if you need to distinguish between absent value and explicit zero value.
func (Int16) UnwrapOr ¶
UnwrapOr returns the stored value if present. Otherwise, returns the provided default value.
Example:
o := NoneInt16() v := o.UnwrapOr(someDefaultInt16)
func (Int16) UnwrapOrElse ¶
UnwrapOrElse returns the stored value if present. Otherwise, calls the provided function and returns its result. Useful when the default value requires computation or side effects.
Example:
o := NoneInt16() v := o.UnwrapOrElse(func() int16 { return computeDefault() })
type Int32 ¶
type Int32 struct {
// contains filtered or unexported fields
}
Int32 represents an optional value of type int32. It can either hold a valid int32 (IsSome == true) or be empty (IsZero == true).
func NoneInt32 ¶
func NoneInt32() Int32
NoneInt32 creates an empty optional Int32 value. The returned Int32 will have IsSome() == false and IsZero() == true.
Example:
o := NoneInt32() if o.IsZero() { fmt.Println("value is absent") }
func SomeInt32 ¶
SomeInt32 creates an optional Int32 with the given int32 value. The returned Int32 will have IsSome() == true and IsZero() == false.
Example:
o := SomeInt32(12) if o.IsSome() { v := o.Unwrap() // v == 12 }
func (*Int32) DecodeMsgpack ¶
DecodeMsgpack decodes a Int32 value from MessagePack format. Supports two input types:
- nil: interpreted as no value (NoneInt32)
- int32: interpreted as a present value (SomeInt32)
Returns an error if the input type is unsupported or decoding fails.
After successful decoding:
- on nil: exists = false, value = default zero value
- on int32: exists = true, value = decoded value
func (Int32) EncodeMsgpack ¶
EncodeMsgpack encodes the Int32 value using MessagePack format. - If the value is present, it is encoded as int32. - If the value is absent (None), it is encoded as nil.
Returns an error if encoding fails.
func (Int32) Get ¶
Get returns the stored value and a boolean flag indicating its presence. If the value is present, returns (value, true). If the value is absent, returns (zero value of int32, false).
Recommended usage:
if value, ok := o.Get(); ok { // use value }
func (Int32) IsNil ¶
IsNil is an alias for IsZero.
This method is provided for compatibility with the msgpack Encoder interface.
func (Int32) IsSome ¶
IsSome returns true if the Int32 contains a value. This indicates the value is explicitly set (not None).
func (Int32) IsZero ¶
IsZero returns true if the Int32 does not contain a value. Equivalent to !IsSome(). Useful for consistency with types where zero value (e.g. 0, false, zero struct) is valid and needs to be distinguished.
func (Int32) MustGet ¶
MustGet returns the stored value if it is present. Panics if the value is absent (i.e., IsZero() == true).
Use with caution — only when you are certain the value exists.
Panics with: "optional value is not set" if no value is set.
func (Int32) Unwrap ¶
Unwrap returns the stored value regardless of presence. If no value is set, returns the zero value for int32.
Warning: Does not check presence. Use IsSome() before calling if you need to distinguish between absent value and explicit zero value.
func (Int32) UnwrapOr ¶
UnwrapOr returns the stored value if present. Otherwise, returns the provided default value.
Example:
o := NoneInt32() v := o.UnwrapOr(someDefaultInt32)
func (Int32) UnwrapOrElse ¶
UnwrapOrElse returns the stored value if present. Otherwise, calls the provided function and returns its result. Useful when the default value requires computation or side effects.
Example:
o := NoneInt32() v := o.UnwrapOrElse(func() int32 { return computeDefault() })
type Int64 ¶
type Int64 struct {
// contains filtered or unexported fields
}
Int64 represents an optional value of type int64. It can either hold a valid int64 (IsSome == true) or be empty (IsZero == true).
func NoneInt64 ¶
func NoneInt64() Int64
NoneInt64 creates an empty optional Int64 value. The returned Int64 will have IsSome() == false and IsZero() == true.
Example:
o := NoneInt64() if o.IsZero() { fmt.Println("value is absent") }
func SomeInt64 ¶
SomeInt64 creates an optional Int64 with the given int64 value. The returned Int64 will have IsSome() == true and IsZero() == false.
Example:
o := SomeInt64(12) if o.IsSome() { v := o.Unwrap() // v == 12 }
func (*Int64) DecodeMsgpack ¶
DecodeMsgpack decodes a Int64 value from MessagePack format. Supports two input types:
- nil: interpreted as no value (NoneInt64)
- int64: interpreted as a present value (SomeInt64)
Returns an error if the input type is unsupported or decoding fails.
After successful decoding:
- on nil: exists = false, value = default zero value
- on int64: exists = true, value = decoded value
func (Int64) EncodeMsgpack ¶
EncodeMsgpack encodes the Int64 value using MessagePack format. - If the value is present, it is encoded as int64. - If the value is absent (None), it is encoded as nil.
Returns an error if encoding fails.
func (Int64) Get ¶
Get returns the stored value and a boolean flag indicating its presence. If the value is present, returns (value, true). If the value is absent, returns (zero value of int64, false).
Recommended usage:
if value, ok := o.Get(); ok { // use value }
func (Int64) IsNil ¶
IsNil is an alias for IsZero.
This method is provided for compatibility with the msgpack Encoder interface.
func (Int64) IsSome ¶
IsSome returns true if the Int64 contains a value. This indicates the value is explicitly set (not None).
func (Int64) IsZero ¶
IsZero returns true if the Int64 does not contain a value. Equivalent to !IsSome(). Useful for consistency with types where zero value (e.g. 0, false, zero struct) is valid and needs to be distinguished.
func (Int64) MustGet ¶
MustGet returns the stored value if it is present. Panics if the value is absent (i.e., IsZero() == true).
Use with caution — only when you are certain the value exists.
Panics with: "optional value is not set" if no value is set.
func (Int64) Unwrap ¶
Unwrap returns the stored value regardless of presence. If no value is set, returns the zero value for int64.
Warning: Does not check presence. Use IsSome() before calling if you need to distinguish between absent value and explicit zero value.
func (Int64) UnwrapOr ¶
UnwrapOr returns the stored value if present. Otherwise, returns the provided default value.
Example:
o := NoneInt64() v := o.UnwrapOr(someDefaultInt64)
func (Int64) UnwrapOrElse ¶
UnwrapOrElse returns the stored value if present. Otherwise, calls the provided function and returns its result. Useful when the default value requires computation or side effects.
Example:
o := NoneInt64() v := o.UnwrapOrElse(func() int64 { return computeDefault() })
type Int8 ¶
type Int8 struct {
// contains filtered or unexported fields
}
Int8 represents an optional value of type int8. It can either hold a valid int8 (IsSome == true) or be empty (IsZero == true).
func NoneInt8 ¶
func NoneInt8() Int8
NoneInt8 creates an empty optional Int8 value. The returned Int8 will have IsSome() == false and IsZero() == true.
Example:
o := NoneInt8() if o.IsZero() { fmt.Println("value is absent") }
func SomeInt8 ¶
SomeInt8 creates an optional Int8 with the given int8 value. The returned Int8 will have IsSome() == true and IsZero() == false.
Example:
o := SomeInt8(12) if o.IsSome() { v := o.Unwrap() // v == 12 }
func (*Int8) DecodeMsgpack ¶
DecodeMsgpack decodes a Int8 value from MessagePack format. Supports two input types:
- nil: interpreted as no value (NoneInt8)
- int8: interpreted as a present value (SomeInt8)
Returns an error if the input type is unsupported or decoding fails.
After successful decoding:
- on nil: exists = false, value = default zero value
- on int8: exists = true, value = decoded value
func (Int8) EncodeMsgpack ¶
EncodeMsgpack encodes the Int8 value using MessagePack format. - If the value is present, it is encoded as int8. - If the value is absent (None), it is encoded as nil.
Returns an error if encoding fails.
func (Int8) Get ¶
Get returns the stored value and a boolean flag indicating its presence. If the value is present, returns (value, true). If the value is absent, returns (zero value of int8, false).
Recommended usage:
if value, ok := o.Get(); ok { // use value }
func (Int8) IsNil ¶
IsNil is an alias for IsZero.
This method is provided for compatibility with the msgpack Encoder interface.
func (Int8) IsSome ¶
IsSome returns true if the Int8 contains a value. This indicates the value is explicitly set (not None).
func (Int8) IsZero ¶
IsZero returns true if the Int8 does not contain a value. Equivalent to !IsSome(). Useful for consistency with types where zero value (e.g. 0, false, zero struct) is valid and needs to be distinguished.
func (Int8) MustGet ¶
MustGet returns the stored value if it is present. Panics if the value is absent (i.e., IsZero() == true).
Use with caution — only when you are certain the value exists.
Panics with: "optional value is not set" if no value is set.
func (Int8) Unwrap ¶
Unwrap returns the stored value regardless of presence. If no value is set, returns the zero value for int8.
Warning: Does not check presence. Use IsSome() before calling if you need to distinguish between absent value and explicit zero value.
func (Int8) UnwrapOr ¶
UnwrapOr returns the stored value if present. Otherwise, returns the provided default value.
Example:
o := NoneInt8() v := o.UnwrapOr(someDefaultInt8)
func (Int8) UnwrapOrElse ¶
UnwrapOrElse returns the stored value if present. Otherwise, calls the provided function and returns its result. Useful when the default value requires computation or side effects.
Example:
o := NoneInt8() v := o.UnwrapOrElse(func() int8 { return computeDefault() })
type String ¶
type String struct {
// contains filtered or unexported fields
}
String represents an optional value of type string. It can either hold a valid string (IsSome == true) or be empty (IsZero == true).
func NoneString ¶
func NoneString() String
NoneString creates an empty optional String value. The returned String will have IsSome() == false and IsZero() == true.
Example:
o := NoneString() if o.IsZero() { fmt.Println("value is absent") }
func SomeString ¶
SomeString creates an optional String with the given string value. The returned String will have IsSome() == true and IsZero() == false.
Example:
o := SomeString("hello") if o.IsSome() { v := o.Unwrap() // v == "hello" }
func (*String) DecodeMsgpack ¶
DecodeMsgpack decodes a String value from MessagePack format. Supports two input types:
- nil: interpreted as no value (NoneString)
- string: interpreted as a present value (SomeString)
Returns an error if the input type is unsupported or decoding fails.
After successful decoding:
- on nil: exists = false, value = default zero value
- on string: exists = true, value = decoded value
func (String) EncodeMsgpack ¶
EncodeMsgpack encodes the String value using MessagePack format. - If the value is present, it is encoded as string. - If the value is absent (None), it is encoded as nil.
Returns an error if encoding fails.
func (String) Get ¶
Get returns the stored value and a boolean flag indicating its presence. If the value is present, returns (value, true). If the value is absent, returns (zero value of string, false).
Recommended usage:
if value, ok := o.Get(); ok { // use value }
func (String) IsNil ¶
IsNil is an alias for IsZero.
This method is provided for compatibility with the msgpack Encoder interface.
func (String) IsSome ¶
IsSome returns true if the String contains a value. This indicates the value is explicitly set (not None).
func (String) IsZero ¶
IsZero returns true if the String does not contain a value. Equivalent to !IsSome(). Useful for consistency with types where zero value (e.g. 0, false, zero struct) is valid and needs to be distinguished.
func (String) MustGet ¶
MustGet returns the stored value if it is present. Panics if the value is absent (i.e., IsZero() == true).
Use with caution — only when you are certain the value exists.
Panics with: "optional value is not set" if no value is set.
func (String) Unwrap ¶
Unwrap returns the stored value regardless of presence. If no value is set, returns the zero value for string.
Warning: Does not check presence. Use IsSome() before calling if you need to distinguish between absent value and explicit zero value.
func (String) UnwrapOr ¶
UnwrapOr returns the stored value if present. Otherwise, returns the provided default value.
Example:
o := NoneString() v := o.UnwrapOr(someDefaultString)
func (String) UnwrapOrElse ¶
UnwrapOrElse returns the stored value if present. Otherwise, calls the provided function and returns its result. Useful when the default value requires computation or side effects.
Example:
o := NoneString() v := o.UnwrapOrElse(func() string { return computeDefault() })
type Uint ¶
type Uint struct {
// contains filtered or unexported fields
}
Uint represents an optional value of type uint. It can either hold a valid uint (IsSome == true) or be empty (IsZero == true).
func NoneUint ¶
func NoneUint() Uint
NoneUint creates an empty optional Uint value. The returned Uint will have IsSome() == false and IsZero() == true.
Example:
o := NoneUint() if o.IsZero() { fmt.Println("value is absent") }
func SomeUint ¶
SomeUint creates an optional Uint with the given uint value. The returned Uint will have IsSome() == true and IsZero() == false.
Example:
o := SomeUint(12) if o.IsSome() { v := o.Unwrap() // v == 12 }
func (*Uint) DecodeMsgpack ¶
DecodeMsgpack decodes a Uint value from MessagePack format. Supports two input types:
- nil: interpreted as no value (NoneUint)
- uint: interpreted as a present value (SomeUint)
Returns an error if the input type is unsupported or decoding fails.
After successful decoding:
- on nil: exists = false, value = default zero value
- on uint: exists = true, value = decoded value
func (Uint) EncodeMsgpack ¶
EncodeMsgpack encodes the Uint value using MessagePack format. - If the value is present, it is encoded as uint. - If the value is absent (None), it is encoded as nil.
Returns an error if encoding fails.
func (Uint) Get ¶
Get returns the stored value and a boolean flag indicating its presence. If the value is present, returns (value, true). If the value is absent, returns (zero value of uint, false).
Recommended usage:
if value, ok := o.Get(); ok { // use value }
func (Uint) IsNil ¶
IsNil is an alias for IsZero.
This method is provided for compatibility with the msgpack Encoder interface.
func (Uint) IsSome ¶
IsSome returns true if the Uint contains a value. This indicates the value is explicitly set (not None).
func (Uint) IsZero ¶
IsZero returns true if the Uint does not contain a value. Equivalent to !IsSome(). Useful for consistency with types where zero value (e.g. 0, false, zero struct) is valid and needs to be distinguished.
func (Uint) MustGet ¶
MustGet returns the stored value if it is present. Panics if the value is absent (i.e., IsZero() == true).
Use with caution — only when you are certain the value exists.
Panics with: "optional value is not set" if no value is set.
func (Uint) Unwrap ¶
Unwrap returns the stored value regardless of presence. If no value is set, returns the zero value for uint.
Warning: Does not check presence. Use IsSome() before calling if you need to distinguish between absent value and explicit zero value.
func (Uint) UnwrapOr ¶
UnwrapOr returns the stored value if present. Otherwise, returns the provided default value.
Example:
o := NoneUint() v := o.UnwrapOr(someDefaultUint)
func (Uint) UnwrapOrElse ¶
UnwrapOrElse returns the stored value if present. Otherwise, calls the provided function and returns its result. Useful when the default value requires computation or side effects.
Example:
o := NoneUint() v := o.UnwrapOrElse(func() uint { return computeDefault() })
type Uint16 ¶
type Uint16 struct {
// contains filtered or unexported fields
}
Uint16 represents an optional value of type uint16. It can either hold a valid uint16 (IsSome == true) or be empty (IsZero == true).
func NoneUint16 ¶
func NoneUint16() Uint16
NoneUint16 creates an empty optional Uint16 value. The returned Uint16 will have IsSome() == false and IsZero() == true.
Example:
o := NoneUint16() if o.IsZero() { fmt.Println("value is absent") }
func SomeUint16 ¶
SomeUint16 creates an optional Uint16 with the given uint16 value. The returned Uint16 will have IsSome() == true and IsZero() == false.
Example:
o := SomeUint16(12) if o.IsSome() { v := o.Unwrap() // v == 12 }
func (*Uint16) DecodeMsgpack ¶
DecodeMsgpack decodes a Uint16 value from MessagePack format. Supports two input types:
- nil: interpreted as no value (NoneUint16)
- uint16: interpreted as a present value (SomeUint16)
Returns an error if the input type is unsupported or decoding fails.
After successful decoding:
- on nil: exists = false, value = default zero value
- on uint16: exists = true, value = decoded value
func (Uint16) EncodeMsgpack ¶
EncodeMsgpack encodes the Uint16 value using MessagePack format. - If the value is present, it is encoded as uint16. - If the value is absent (None), it is encoded as nil.
Returns an error if encoding fails.
func (Uint16) Get ¶
Get returns the stored value and a boolean flag indicating its presence. If the value is present, returns (value, true). If the value is absent, returns (zero value of uint16, false).
Recommended usage:
if value, ok := o.Get(); ok { // use value }
func (Uint16) IsNil ¶
IsNil is an alias for IsZero.
This method is provided for compatibility with the msgpack Encoder interface.
func (Uint16) IsSome ¶
IsSome returns true if the Uint16 contains a value. This indicates the value is explicitly set (not None).
func (Uint16) IsZero ¶
IsZero returns true if the Uint16 does not contain a value. Equivalent to !IsSome(). Useful for consistency with types where zero value (e.g. 0, false, zero struct) is valid and needs to be distinguished.
func (Uint16) MustGet ¶
MustGet returns the stored value if it is present. Panics if the value is absent (i.e., IsZero() == true).
Use with caution — only when you are certain the value exists.
Panics with: "optional value is not set" if no value is set.
func (Uint16) Unwrap ¶
Unwrap returns the stored value regardless of presence. If no value is set, returns the zero value for uint16.
Warning: Does not check presence. Use IsSome() before calling if you need to distinguish between absent value and explicit zero value.
func (Uint16) UnwrapOr ¶
UnwrapOr returns the stored value if present. Otherwise, returns the provided default value.
Example:
o := NoneUint16() v := o.UnwrapOr(someDefaultUint16)
func (Uint16) UnwrapOrElse ¶
UnwrapOrElse returns the stored value if present. Otherwise, calls the provided function and returns its result. Useful when the default value requires computation or side effects.
Example:
o := NoneUint16() v := o.UnwrapOrElse(func() uint16 { return computeDefault() })
type Uint32 ¶
type Uint32 struct {
// contains filtered or unexported fields
}
Uint32 represents an optional value of type uint32. It can either hold a valid uint32 (IsSome == true) or be empty (IsZero == true).
func NoneUint32 ¶
func NoneUint32() Uint32
NoneUint32 creates an empty optional Uint32 value. The returned Uint32 will have IsSome() == false and IsZero() == true.
Example:
o := NoneUint32() if o.IsZero() { fmt.Println("value is absent") }
func SomeUint32 ¶
SomeUint32 creates an optional Uint32 with the given uint32 value. The returned Uint32 will have IsSome() == true and IsZero() == false.
Example:
o := SomeUint32(12) if o.IsSome() { v := o.Unwrap() // v == 12 }
func (*Uint32) DecodeMsgpack ¶
DecodeMsgpack decodes a Uint32 value from MessagePack format. Supports two input types:
- nil: interpreted as no value (NoneUint32)
- uint32: interpreted as a present value (SomeUint32)
Returns an error if the input type is unsupported or decoding fails.
After successful decoding:
- on nil: exists = false, value = default zero value
- on uint32: exists = true, value = decoded value
func (Uint32) EncodeMsgpack ¶
EncodeMsgpack encodes the Uint32 value using MessagePack format. - If the value is present, it is encoded as uint32. - If the value is absent (None), it is encoded as nil.
Returns an error if encoding fails.
func (Uint32) Get ¶
Get returns the stored value and a boolean flag indicating its presence. If the value is present, returns (value, true). If the value is absent, returns (zero value of uint32, false).
Recommended usage:
if value, ok := o.Get(); ok { // use value }
func (Uint32) IsNil ¶
IsNil is an alias for IsZero.
This method is provided for compatibility with the msgpack Encoder interface.
func (Uint32) IsSome ¶
IsSome returns true if the Uint32 contains a value. This indicates the value is explicitly set (not None).
func (Uint32) IsZero ¶
IsZero returns true if the Uint32 does not contain a value. Equivalent to !IsSome(). Useful for consistency with types where zero value (e.g. 0, false, zero struct) is valid and needs to be distinguished.
func (Uint32) MustGet ¶
MustGet returns the stored value if it is present. Panics if the value is absent (i.e., IsZero() == true).
Use with caution — only when you are certain the value exists.
Panics with: "optional value is not set" if no value is set.
func (Uint32) Unwrap ¶
Unwrap returns the stored value regardless of presence. If no value is set, returns the zero value for uint32.
Warning: Does not check presence. Use IsSome() before calling if you need to distinguish between absent value and explicit zero value.
func (Uint32) UnwrapOr ¶
UnwrapOr returns the stored value if present. Otherwise, returns the provided default value.
Example:
o := NoneUint32() v := o.UnwrapOr(someDefaultUint32)
func (Uint32) UnwrapOrElse ¶
UnwrapOrElse returns the stored value if present. Otherwise, calls the provided function and returns its result. Useful when the default value requires computation or side effects.
Example:
o := NoneUint32() v := o.UnwrapOrElse(func() uint32 { return computeDefault() })
type Uint64 ¶
type Uint64 struct {
// contains filtered or unexported fields
}
Uint64 represents an optional value of type uint64. It can either hold a valid uint64 (IsSome == true) or be empty (IsZero == true).
func NoneUint64 ¶
func NoneUint64() Uint64
NoneUint64 creates an empty optional Uint64 value. The returned Uint64 will have IsSome() == false and IsZero() == true.
Example:
o := NoneUint64() if o.IsZero() { fmt.Println("value is absent") }
func SomeUint64 ¶
SomeUint64 creates an optional Uint64 with the given uint64 value. The returned Uint64 will have IsSome() == true and IsZero() == false.
Example:
o := SomeUint64(12) if o.IsSome() { v := o.Unwrap() // v == 12 }
func (*Uint64) DecodeMsgpack ¶
DecodeMsgpack decodes a Uint64 value from MessagePack format. Supports two input types:
- nil: interpreted as no value (NoneUint64)
- uint64: interpreted as a present value (SomeUint64)
Returns an error if the input type is unsupported or decoding fails.
After successful decoding:
- on nil: exists = false, value = default zero value
- on uint64: exists = true, value = decoded value
func (Uint64) EncodeMsgpack ¶
EncodeMsgpack encodes the Uint64 value using MessagePack format. - If the value is present, it is encoded as uint64. - If the value is absent (None), it is encoded as nil.
Returns an error if encoding fails.
func (Uint64) Get ¶
Get returns the stored value and a boolean flag indicating its presence. If the value is present, returns (value, true). If the value is absent, returns (zero value of uint64, false).
Recommended usage:
if value, ok := o.Get(); ok { // use value }
func (Uint64) IsNil ¶
IsNil is an alias for IsZero.
This method is provided for compatibility with the msgpack Encoder interface.
func (Uint64) IsSome ¶
IsSome returns true if the Uint64 contains a value. This indicates the value is explicitly set (not None).
func (Uint64) IsZero ¶
IsZero returns true if the Uint64 does not contain a value. Equivalent to !IsSome(). Useful for consistency with types where zero value (e.g. 0, false, zero struct) is valid and needs to be distinguished.
func (Uint64) MustGet ¶
MustGet returns the stored value if it is present. Panics if the value is absent (i.e., IsZero() == true).
Use with caution — only when you are certain the value exists.
Panics with: "optional value is not set" if no value is set.
func (Uint64) Unwrap ¶
Unwrap returns the stored value regardless of presence. If no value is set, returns the zero value for uint64.
Warning: Does not check presence. Use IsSome() before calling if you need to distinguish between absent value and explicit zero value.
func (Uint64) UnwrapOr ¶
UnwrapOr returns the stored value if present. Otherwise, returns the provided default value.
Example:
o := NoneUint64() v := o.UnwrapOr(someDefaultUint64)
func (Uint64) UnwrapOrElse ¶
UnwrapOrElse returns the stored value if present. Otherwise, calls the provided function and returns its result. Useful when the default value requires computation or side effects.
Example:
o := NoneUint64() v := o.UnwrapOrElse(func() uint64 { return computeDefault() })
type Uint8 ¶
type Uint8 struct {
// contains filtered or unexported fields
}
Uint8 represents an optional value of type uint8. It can either hold a valid uint8 (IsSome == true) or be empty (IsZero == true).
func NoneUint8 ¶
func NoneUint8() Uint8
NoneUint8 creates an empty optional Uint8 value. The returned Uint8 will have IsSome() == false and IsZero() == true.
Example:
o := NoneUint8() if o.IsZero() { fmt.Println("value is absent") }
func SomeUint8 ¶
SomeUint8 creates an optional Uint8 with the given uint8 value. The returned Uint8 will have IsSome() == true and IsZero() == false.
Example:
o := SomeUint8(12) if o.IsSome() { v := o.Unwrap() // v == 12 }
func (*Uint8) DecodeMsgpack ¶
DecodeMsgpack decodes a Uint8 value from MessagePack format. Supports two input types:
- nil: interpreted as no value (NoneUint8)
- uint8: interpreted as a present value (SomeUint8)
Returns an error if the input type is unsupported or decoding fails.
After successful decoding:
- on nil: exists = false, value = default zero value
- on uint8: exists = true, value = decoded value
func (Uint8) EncodeMsgpack ¶
EncodeMsgpack encodes the Uint8 value using MessagePack format. - If the value is present, it is encoded as uint8. - If the value is absent (None), it is encoded as nil.
Returns an error if encoding fails.
func (Uint8) Get ¶
Get returns the stored value and a boolean flag indicating its presence. If the value is present, returns (value, true). If the value is absent, returns (zero value of uint8, false).
Recommended usage:
if value, ok := o.Get(); ok { // use value }
func (Uint8) IsNil ¶
IsNil is an alias for IsZero.
This method is provided for compatibility with the msgpack Encoder interface.
func (Uint8) IsSome ¶
IsSome returns true if the Uint8 contains a value. This indicates the value is explicitly set (not None).
func (Uint8) IsZero ¶
IsZero returns true if the Uint8 does not contain a value. Equivalent to !IsSome(). Useful for consistency with types where zero value (e.g. 0, false, zero struct) is valid and needs to be distinguished.
func (Uint8) MustGet ¶
MustGet returns the stored value if it is present. Panics if the value is absent (i.e., IsZero() == true).
Use with caution — only when you are certain the value exists.
Panics with: "optional value is not set" if no value is set.
func (Uint8) Unwrap ¶
Unwrap returns the stored value regardless of presence. If no value is set, returns the zero value for uint8.
Warning: Does not check presence. Use IsSome() before calling if you need to distinguish between absent value and explicit zero value.
func (Uint8) UnwrapOr ¶
UnwrapOr returns the stored value if present. Otherwise, returns the provided default value.
Example:
o := NoneUint8() v := o.UnwrapOr(someDefaultUint8)
func (Uint8) UnwrapOrElse ¶
UnwrapOrElse returns the stored value if present. Otherwise, calls the provided function and returns its result. Useful when the default value requires computation or side effects.
Example:
o := NoneUint8() v := o.UnwrapOrElse(func() uint8 { return computeDefault() })
Source Files
¶
Directories
¶
Path | Synopsis |
---|---|
cmd
|
|
generator
command
|
|
gentypes
command
Package main is a binary, that generates optional types for types with support for MessagePack Extensions fast encoding/decoding.
|
Package main is a binary, that generates optional types for types with support for MessagePack Extensions fast encoding/decoding. |
gentypes/extractor
Package extractor is a package, that extracts type specs and methods from given ast tree.
|
Package extractor is a package, that extracts type specs and methods from given ast tree. |
gentypes/generator
Package generator is a package that defines how code should be generated.
|
Package generator is a package that defines how code should be generated. |
gentypes/internal/test
Package test contains testing types and scenarios.
|
Package test contains testing types and scenarios. |
gentypes/internal/test/subpackage
Package subpackage contains a hidden type for testing type aliases generation.
|
Package subpackage contains a hidden type for testing type aliases generation. |