option

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2025 License: BSD-2-Clause Imports: 3 Imported by: 0

README

Go Reference Actions Status Code Coverage Telegram EN Telegram RU

go-option: library to work with optional types

Package option implements effective and useful instruments to work with optional types in Go. It eliminates code doubling and provides high performance due to:

  • no memory allocations
  • serialization without reflection (at least for pre-generated types)
  • support for basic and custom types

Table of contents

Installation

go install github.com/tarantool/go-option@latest

Documentation

You could run the godoc server on localhost:6060 with the command:

make godoc_run

And open the generated documentation in another terminal or use the link:

make godoc_open

Quick start

Using pre-generated optional types

Generated types follow the pattern Optional and provide methods for working with optional values:

// Create an optional with a value.
opt := SomeOptionalString("hello")

// Check if a value is present.
if opt.IsSome() {
    value := opt.Unwrap()
    fmt.Println(value)
}

// Use a default value if none.
value := opt.UnwrapOr("default")

// Encode to MessagePack.
err := opt.EncodeMsgpack(encoder)
Usage with go-tarantool

It may be necessary to use an optional type in a structure. For example, to distinguish between a nil value and a missing one.

package main

import (
  "github.com/tarantool/go-option"
  tarantool "github.com/tarantool/go-tarantool/v2"
)

type User struct {
	// may be used in conjunciton with 'msgpack:",omitempty"' directive to skip fields
    _msgpack struct{} `msgpack:",asArray"` //nolint: structcheck,unused
    Name     string
    Phone    option.String
}

func main() {
    var conn *tarantool.Doer
    // Initialize tarantool connection

    // Imagine you get a slice of users from Tarantool.
    users := []User{
        {
            Name: "Nosipho Nnenne",
            Phone: option.SomeString("+15056463408"),
        },
        {
            Name:  "Maryamu Efe",
            Phone: option.NoneString(),
        },
        {
            Name: "Venera Okafor",
        },
    }

    for id, user := range users {
        conn.Do(
            tarantool.NewInsertRequest("users").Tuple(user),
        )
    }
}

Gentype Utility

A Go code generator for creating optional types with MessagePack serialization support.

Overview

Gentype generates wrapper types for various Go primitives and custom types that implement optional (some/none) semantics with full MessagePack serialization capabilities. These generated types are useful for representing values that may or may not be present, while ensuring proper encoding and decoding when using MessagePack.

Features
  • Generates optional types for built-in types (bool, int, float, string, etc.)
  • Supports custom types with MessagePack extension serialization
  • Provides common optional type operations:
    • SomeXxx(value) - Create an optional with a value
    • NoneXxx() - Create an empty optional
    • Unwrap(), UnwrapOr(), UnwrapOrElse() - Value extraction
    • IsSome(), IsNone() - Presence checking
  • Full MessagePack CustomEncoder and CustomDecoder implementation
  • Type-safe operations
Gentype installation
go install github.com/tarantool/go-option/cmd/gentypes@latest
# OR (for go version 1.24+)
go get -tool github.com/tarantool/go-option/cmd/gentypes@latest
Generating Optional Types

To generate optional types for existing types in a package:

gentypes -package ./path/to/package -ext-code 123
# OR (for go version 1.24+)
go tool gentypes -package ./path/to/package -ext-code 123

Or you can use it to generate file from go:

//go:generate go run github.com/tarantool/go-option/cmd/gentypes@latest -ext-code 123
// OR (for go version 1.24+)
//go:generate go tool gentypes -ext-code 123

Flags:

  • -package: Path to the Go package containing types to wrap (default: ".")
  • -ext-code: MessagePack extension code to use for custom types (must be between -128 and 127, no default value)
  • -verbose: Enable verbose output (default: false)
  • -force: Ignore absence of marshal/unmarshal methods on type (default: false). Helpful for types from third-party modules.
  • -imports: Add imports to generated file (default is empty). Helpful for types from third-party modules.
  • -marshal-func: func that should do marshaling (default is MarshalMsgpack method). Helpful for types from third-party modules. Should be func of type func(v T) ([]byte, error) and should be located in the same dir or should be imported.
  • -unmarshal-func: func that should do unmarshalling (default is UnmarshalMsgpack method). Helpful for types from third-party modules. Should be func of type func(v *T, data []byte) error and should be located in the same dir or should be imported.
Generating Optional Types for Third-Party Modules

Sometimes you need to generate an optional type for a type from a third-party module, and you can't add MarshalMsgpack/UnmarshalMsgpack methods to it. In this case, you can use the -force, -imports, -marshal-func, and -unmarshal-func flags.

For example, to generate an optional type for github.com/google/uuid.UUID:

  1. Create a file with marshal and unmarshal functions for the third-party type. For example, uuid.go:

    package main
    
    import (
        "errors"
    
        "github.com/google/uuid"
    )
    
    func encodeUUID(uuid uuid.UUID) ([]byte, error) {
        return uuid[:], nil
    }
    
    var (
        ErrInvalidLength = errors.New("invalid length")
    )
    
    func decodeUUID(uuid *uuid.UUID, data []byte) error {
        if len(data) != len(uuid) {
            return ErrInvalidLength
        }
        copy(uuid[:], data)
        return nil
    }
    
  2. Use the following go:generate command:

    //go:generate go run github.com/tarantool/go-option/cmd/gentypes@latest -package . -imports "github.com/google/uuid" -type UUID -marshal-func "encodeUUID" -unmarshal-func "decodeUUID" -force -ext-code 100
    
Using Generated Types

Generated types follow the pattern Optional and provide methods for working with optional values:

// Create an optional with a value.
opt := SomeOptionalString("hello")

// Check if a value is present.
if opt.IsSome() {
    value := opt.Unwrap()
    fmt.Println(value)
}

// Use a default value if none.
value := opt.UnwrapOr("default")

// Encode to MessagePack.
err := opt.EncodeMsgpack(encoder)

Development

You could use our Makefile targets:

make codespell
make test
make testrace
make coveralls-deps
make coveralls
make coverage
Run tests

To run default set of tests directly:

go test ./... -count=1

License

BSD 2-Clause License

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

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

func SomeBool(value bool) Bool

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

func (o *Bool) DecodeMsgpack(decoder *msgpack.Decoder) error

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

func (o Bool) EncodeMsgpack(encoder *msgpack.Encoder) error

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

func (o Bool) Get() (bool, bool)

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

func (o Bool) IsNil() bool

IsNil is an alias for IsZero.

This method is provided for compatibility with the msgpack Encoder interface.

func (Bool) IsSome

func (o Bool) IsSome() bool

IsSome returns true if the Bool contains a value. This indicates the value is explicitly set (not None).

func (Bool) IsZero

func (o Bool) IsZero() bool

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

func (o Bool) MustGet() bool

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

func (o Bool) Unwrap() bool

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

func (o Bool) UnwrapOr(defaultValue bool) bool

UnwrapOr returns the stored value if present. Otherwise, returns the provided default value.

Example:

o := NoneBool()
v := o.UnwrapOr(someDefaultBool)

func (Bool) UnwrapOrElse

func (o Bool) UnwrapOrElse(defaultValue func() bool) bool

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

func SomeByte(value byte) Byte

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

func (o *Byte) DecodeMsgpack(decoder *msgpack.Decoder) error

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

func (o Byte) EncodeMsgpack(encoder *msgpack.Encoder) error

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

func (o Byte) Get() (byte, bool)

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

func (o Byte) IsNil() bool

IsNil is an alias for IsZero.

This method is provided for compatibility with the msgpack Encoder interface.

func (Byte) IsSome

func (o Byte) IsSome() bool

IsSome returns true if the Byte contains a value. This indicates the value is explicitly set (not None).

func (Byte) IsZero

func (o Byte) IsZero() bool

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

func (o Byte) MustGet() byte

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

func (o Byte) Unwrap() byte

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

func (o Byte) UnwrapOr(defaultValue byte) byte

UnwrapOr returns the stored value if present. Otherwise, returns the provided default value.

Example:

o := NoneByte()
v := o.UnwrapOr(someDefaultByte)

func (Byte) UnwrapOrElse

func (o Byte) UnwrapOrElse(defaultValue func() byte) byte

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

func SomeBytes(value []byte) Bytes

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

func (o *Bytes) DecodeMsgpack(decoder *msgpack.Decoder) error

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

func (o Bytes) EncodeMsgpack(encoder *msgpack.Encoder) error

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

func (o Bytes) Get() ([]byte, bool)

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

func (o Bytes) IsNil() bool

IsNil is an alias for IsZero.

This method is provided for compatibility with the msgpack Encoder interface.

func (Bytes) IsSome

func (o Bytes) IsSome() bool

IsSome returns true if the Bytes contains a value. This indicates the value is explicitly set (not None).

func (Bytes) IsZero

func (o Bytes) IsZero() bool

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

func (o Bytes) MustGet() []byte

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

func (o Bytes) Unwrap() []byte

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

func (o Bytes) UnwrapOr(defaultValue []byte) []byte

UnwrapOr returns the stored value if present. Otherwise, returns the provided default value.

Example:

o := NoneBytes()
v := o.UnwrapOr(someDefaultBytes)

func (Bytes) UnwrapOrElse

func (o Bytes) UnwrapOrElse(defaultValue func() []byte) []byte

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

type DecodeError struct {
	Type   string
	Code   Byte
	Parent error
}

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

type EncodeError struct {
	Type   string
	Parent error
}

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

func SomeFloat32(value float32) Float32

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

func (o *Float32) DecodeMsgpack(decoder *msgpack.Decoder) error

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

func (o Float32) EncodeMsgpack(encoder *msgpack.Encoder) error

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

func (o Float32) Get() (float32, bool)

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

func (o Float32) IsNil() bool

IsNil is an alias for IsZero.

This method is provided for compatibility with the msgpack Encoder interface.

func (Float32) IsSome

func (o Float32) IsSome() bool

IsSome returns true if the Float32 contains a value. This indicates the value is explicitly set (not None).

func (Float32) IsZero

func (o Float32) IsZero() bool

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

func (o Float32) MustGet() float32

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

func (o Float32) Unwrap() float32

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

func (o Float32) UnwrapOr(defaultValue float32) float32

UnwrapOr returns the stored value if present. Otherwise, returns the provided default value.

Example:

o := NoneFloat32()
v := o.UnwrapOr(someDefaultFloat32)

func (Float32) UnwrapOrElse

func (o Float32) UnwrapOrElse(defaultValue func() float32) float32

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

func SomeFloat64(value float64) Float64

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

func (o *Float64) DecodeMsgpack(decoder *msgpack.Decoder) error

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

func (o Float64) EncodeMsgpack(encoder *msgpack.Encoder) error

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

func (o Float64) Get() (float64, bool)

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

func (o Float64) IsNil() bool

IsNil is an alias for IsZero.

This method is provided for compatibility with the msgpack Encoder interface.

func (Float64) IsSome

func (o Float64) IsSome() bool

IsSome returns true if the Float64 contains a value. This indicates the value is explicitly set (not None).

func (Float64) IsZero

func (o Float64) IsZero() bool

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

func (o Float64) MustGet() float64

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

func (o Float64) Unwrap() float64

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

func (o Float64) UnwrapOr(defaultValue float64) float64

UnwrapOr returns the stored value if present. Otherwise, returns the provided default value.

Example:

o := NoneFloat64()
v := o.UnwrapOr(someDefaultFloat64)

func (Float64) UnwrapOrElse

func (o Float64) UnwrapOrElse(defaultValue func() float64) float64

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

func None[T any]() Generic[T]

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

func Some[T any](value T) Generic[T]

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

func (o *Generic[T]) DecodeMsgpack(decoder *msgpack.Decoder) error

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

func (o Generic[T]) EncodeMsgpack(encoder *msgpack.Encoder) error

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

func (o Generic[T]) Get() (T, bool)

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

func (o Generic[T]) IsNil() bool

IsNil is an alias for IsZero.

This method is provided for compatibility with the msgpack Encoder interface.

func (Generic[T]) IsSome

func (o Generic[T]) IsSome() bool

IsSome returns true if the optional contains a value.

Example:

if opt.IsSome() {
    // safely access value
}

func (Generic[T]) IsZero

func (o Generic[T]) IsZero() bool

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

func SomeInt(value int) Int

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

func (o *Int) DecodeMsgpack(decoder *msgpack.Decoder) error

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

func (o Int) EncodeMsgpack(encoder *msgpack.Encoder) error

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

func (o Int) Get() (int, bool)

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

func (o Int) IsNil() bool

IsNil is an alias for IsZero.

This method is provided for compatibility with the msgpack Encoder interface.

func (Int) IsSome

func (o Int) IsSome() bool

IsSome returns true if the Int contains a value. This indicates the value is explicitly set (not None).

func (Int) IsZero

func (o Int) IsZero() bool

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

func (o Int) MustGet() int

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

func (o Int) Unwrap() int

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

func (o Int) UnwrapOr(defaultValue int) int

UnwrapOr returns the stored value if present. Otherwise, returns the provided default value.

Example:

o := NoneInt()
v := o.UnwrapOr(someDefaultInt)

func (Int) UnwrapOrElse

func (o Int) UnwrapOrElse(defaultValue func() int) int

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

func SomeInt16(value int16) Int16

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

func (o *Int16) DecodeMsgpack(decoder *msgpack.Decoder) error

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

func (o Int16) EncodeMsgpack(encoder *msgpack.Encoder) error

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

func (o Int16) Get() (int16, bool)

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

func (o Int16) IsNil() bool

IsNil is an alias for IsZero.

This method is provided for compatibility with the msgpack Encoder interface.

func (Int16) IsSome

func (o Int16) IsSome() bool

IsSome returns true if the Int16 contains a value. This indicates the value is explicitly set (not None).

func (Int16) IsZero

func (o Int16) IsZero() bool

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

func (o Int16) MustGet() int16

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

func (o Int16) Unwrap() int16

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

func (o Int16) UnwrapOr(defaultValue int16) int16

UnwrapOr returns the stored value if present. Otherwise, returns the provided default value.

Example:

o := NoneInt16()
v := o.UnwrapOr(someDefaultInt16)

func (Int16) UnwrapOrElse

func (o Int16) UnwrapOrElse(defaultValue func() int16) int16

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

func SomeInt32(value int32) Int32

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

func (o *Int32) DecodeMsgpack(decoder *msgpack.Decoder) error

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

func (o Int32) EncodeMsgpack(encoder *msgpack.Encoder) error

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

func (o Int32) Get() (int32, bool)

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

func (o Int32) IsNil() bool

IsNil is an alias for IsZero.

This method is provided for compatibility with the msgpack Encoder interface.

func (Int32) IsSome

func (o Int32) IsSome() bool

IsSome returns true if the Int32 contains a value. This indicates the value is explicitly set (not None).

func (Int32) IsZero

func (o Int32) IsZero() bool

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

func (o Int32) MustGet() int32

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

func (o Int32) Unwrap() int32

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

func (o Int32) UnwrapOr(defaultValue int32) int32

UnwrapOr returns the stored value if present. Otherwise, returns the provided default value.

Example:

o := NoneInt32()
v := o.UnwrapOr(someDefaultInt32)

func (Int32) UnwrapOrElse

func (o Int32) UnwrapOrElse(defaultValue func() int32) int32

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

func SomeInt64(value int64) Int64

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

func (o *Int64) DecodeMsgpack(decoder *msgpack.Decoder) error

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

func (o Int64) EncodeMsgpack(encoder *msgpack.Encoder) error

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

func (o Int64) Get() (int64, bool)

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

func (o Int64) IsNil() bool

IsNil is an alias for IsZero.

This method is provided for compatibility with the msgpack Encoder interface.

func (Int64) IsSome

func (o Int64) IsSome() bool

IsSome returns true if the Int64 contains a value. This indicates the value is explicitly set (not None).

func (Int64) IsZero

func (o Int64) IsZero() bool

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

func (o Int64) MustGet() int64

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

func (o Int64) Unwrap() int64

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

func (o Int64) UnwrapOr(defaultValue int64) int64

UnwrapOr returns the stored value if present. Otherwise, returns the provided default value.

Example:

o := NoneInt64()
v := o.UnwrapOr(someDefaultInt64)

func (Int64) UnwrapOrElse

func (o Int64) UnwrapOrElse(defaultValue func() int64) int64

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

func SomeInt8(value int8) Int8

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

func (o *Int8) DecodeMsgpack(decoder *msgpack.Decoder) error

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

func (o Int8) EncodeMsgpack(encoder *msgpack.Encoder) error

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

func (o Int8) Get() (int8, bool)

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

func (o Int8) IsNil() bool

IsNil is an alias for IsZero.

This method is provided for compatibility with the msgpack Encoder interface.

func (Int8) IsSome

func (o Int8) IsSome() bool

IsSome returns true if the Int8 contains a value. This indicates the value is explicitly set (not None).

func (Int8) IsZero

func (o Int8) IsZero() bool

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

func (o Int8) MustGet() int8

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

func (o Int8) Unwrap() int8

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

func (o Int8) UnwrapOr(defaultValue int8) int8

UnwrapOr returns the stored value if present. Otherwise, returns the provided default value.

Example:

o := NoneInt8()
v := o.UnwrapOr(someDefaultInt8)

func (Int8) UnwrapOrElse

func (o Int8) UnwrapOrElse(defaultValue func() int8) int8

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

func SomeString(value string) String

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

func (o *String) DecodeMsgpack(decoder *msgpack.Decoder) error

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

func (o String) EncodeMsgpack(encoder *msgpack.Encoder) error

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

func (o String) Get() (string, bool)

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

func (o String) IsNil() bool

IsNil is an alias for IsZero.

This method is provided for compatibility with the msgpack Encoder interface.

func (String) IsSome

func (o String) IsSome() bool

IsSome returns true if the String contains a value. This indicates the value is explicitly set (not None).

func (String) IsZero

func (o String) IsZero() bool

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

func (o String) MustGet() string

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

func (o String) Unwrap() string

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

func (o String) UnwrapOr(defaultValue string) string

UnwrapOr returns the stored value if present. Otherwise, returns the provided default value.

Example:

o := NoneString()
v := o.UnwrapOr(someDefaultString)

func (String) UnwrapOrElse

func (o String) UnwrapOrElse(defaultValue func() string) string

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

func SomeUint(value uint) Uint

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

func (o *Uint) DecodeMsgpack(decoder *msgpack.Decoder) error

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

func (o Uint) EncodeMsgpack(encoder *msgpack.Encoder) error

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

func (o Uint) Get() (uint, bool)

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

func (o Uint) IsNil() bool

IsNil is an alias for IsZero.

This method is provided for compatibility with the msgpack Encoder interface.

func (Uint) IsSome

func (o Uint) IsSome() bool

IsSome returns true if the Uint contains a value. This indicates the value is explicitly set (not None).

func (Uint) IsZero

func (o Uint) IsZero() bool

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

func (o Uint) MustGet() uint

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

func (o Uint) Unwrap() uint

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

func (o Uint) UnwrapOr(defaultValue uint) uint

UnwrapOr returns the stored value if present. Otherwise, returns the provided default value.

Example:

o := NoneUint()
v := o.UnwrapOr(someDefaultUint)

func (Uint) UnwrapOrElse

func (o Uint) UnwrapOrElse(defaultValue func() uint) uint

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

func SomeUint16(value uint16) Uint16

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

func (o *Uint16) DecodeMsgpack(decoder *msgpack.Decoder) error

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

func (o Uint16) EncodeMsgpack(encoder *msgpack.Encoder) error

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

func (o Uint16) Get() (uint16, bool)

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

func (o Uint16) IsNil() bool

IsNil is an alias for IsZero.

This method is provided for compatibility with the msgpack Encoder interface.

func (Uint16) IsSome

func (o Uint16) IsSome() bool

IsSome returns true if the Uint16 contains a value. This indicates the value is explicitly set (not None).

func (Uint16) IsZero

func (o Uint16) IsZero() bool

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

func (o Uint16) MustGet() uint16

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

func (o Uint16) Unwrap() uint16

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

func (o Uint16) UnwrapOr(defaultValue uint16) uint16

UnwrapOr returns the stored value if present. Otherwise, returns the provided default value.

Example:

o := NoneUint16()
v := o.UnwrapOr(someDefaultUint16)

func (Uint16) UnwrapOrElse

func (o Uint16) UnwrapOrElse(defaultValue func() uint16) uint16

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

func SomeUint32(value uint32) Uint32

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

func (o *Uint32) DecodeMsgpack(decoder *msgpack.Decoder) error

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

func (o Uint32) EncodeMsgpack(encoder *msgpack.Encoder) error

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

func (o Uint32) Get() (uint32, bool)

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

func (o Uint32) IsNil() bool

IsNil is an alias for IsZero.

This method is provided for compatibility with the msgpack Encoder interface.

func (Uint32) IsSome

func (o Uint32) IsSome() bool

IsSome returns true if the Uint32 contains a value. This indicates the value is explicitly set (not None).

func (Uint32) IsZero

func (o Uint32) IsZero() bool

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

func (o Uint32) MustGet() uint32

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

func (o Uint32) Unwrap() uint32

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

func (o Uint32) UnwrapOr(defaultValue uint32) uint32

UnwrapOr returns the stored value if present. Otherwise, returns the provided default value.

Example:

o := NoneUint32()
v := o.UnwrapOr(someDefaultUint32)

func (Uint32) UnwrapOrElse

func (o Uint32) UnwrapOrElse(defaultValue func() uint32) uint32

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

func SomeUint64(value uint64) Uint64

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

func (o *Uint64) DecodeMsgpack(decoder *msgpack.Decoder) error

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

func (o Uint64) EncodeMsgpack(encoder *msgpack.Encoder) error

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

func (o Uint64) Get() (uint64, bool)

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

func (o Uint64) IsNil() bool

IsNil is an alias for IsZero.

This method is provided for compatibility with the msgpack Encoder interface.

func (Uint64) IsSome

func (o Uint64) IsSome() bool

IsSome returns true if the Uint64 contains a value. This indicates the value is explicitly set (not None).

func (Uint64) IsZero

func (o Uint64) IsZero() bool

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

func (o Uint64) MustGet() uint64

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

func (o Uint64) Unwrap() uint64

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

func (o Uint64) UnwrapOr(defaultValue uint64) uint64

UnwrapOr returns the stored value if present. Otherwise, returns the provided default value.

Example:

o := NoneUint64()
v := o.UnwrapOr(someDefaultUint64)

func (Uint64) UnwrapOrElse

func (o Uint64) UnwrapOrElse(defaultValue func() uint64) uint64

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

func SomeUint8(value uint8) Uint8

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

func (o *Uint8) DecodeMsgpack(decoder *msgpack.Decoder) error

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

func (o Uint8) EncodeMsgpack(encoder *msgpack.Encoder) error

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

func (o Uint8) Get() (uint8, bool)

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

func (o Uint8) IsNil() bool

IsNil is an alias for IsZero.

This method is provided for compatibility with the msgpack Encoder interface.

func (Uint8) IsSome

func (o Uint8) IsSome() bool

IsSome returns true if the Uint8 contains a value. This indicates the value is explicitly set (not None).

func (Uint8) IsZero

func (o Uint8) IsZero() bool

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

func (o Uint8) MustGet() uint8

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

func (o Uint8) Unwrap() uint8

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

func (o Uint8) UnwrapOr(defaultValue uint8) uint8

UnwrapOr returns the stored value if present. Otherwise, returns the provided default value.

Example:

o := NoneUint8()
v := o.UnwrapOr(someDefaultUint8)

func (Uint8) UnwrapOrElse

func (o Uint8) UnwrapOrElse(defaultValue func() uint8) uint8

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() })

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.

Jump to

Keyboard shortcuts

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