option

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2025 License: BSD-2-Clause Imports: 3 Imported by: 5

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 := option.SomeString("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)
Using generic approach
type SomeType struct {
    name string
    number int
}

// Create an optional with a value.
opt := option.Some(SomeType{"hello", 42})

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

// Use a default value if none.
value := opt.UnwrapOr(SomeType{"default", 0})

// 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(), IsNil() - 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 provide methods for working with optional values and 2 constructors for every single type: Some<TypeName> creates option with some value and None<TypeName> creates the empty one ( is the original type name started with upper case):

// Create an optional with a value.
opt := option.SomeString("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

Benchmarking

Along with the approach supplied with go-option library pointer-based and slice-based approaches were benchmarked as well.

Init + Get (empty value)
# int
BenchmarkNoneInt/Typed-8        	560566400	 2.200 ns/op	 0 B/op	  0 allocs/op
BenchmarkNoneInt/Generic-8      	543332625	 2.193 ns/op	 0 B/op	  0 allocs/op
BenchmarkNoneInt/GenericPtr-8   	487631254	 2.474 ns/op	 0 B/op	  0 allocs/op
BenchmarkNoneInt/GenericSlice-8 	441513422	 2.608 ns/op	 0 B/op	  0 allocs/op
# string
BenchmarkNoneString/Typed-8        	170894025	 6.545 ns/op	 0 B/op	 0 allocs/op
BenchmarkNoneString/Generic-8      	185572758	 6.451 ns/op	 0 B/op	 0 allocs/op
BenchmarkNoneString/GenericPtr-8   	159143874	 7.459 ns/op	 0 B/op	 0 allocs/op
BenchmarkNoneString/GenericSlice-8 	173419598	 6.708 ns/op	 0 B/op	 0 allocs/op
# struct
BenchmarkNoneStruct/Typed-8        	384845384	 3.107 ns/op	 0 B/op	  0 allocs/op
BenchmarkNoneStruct/Generic-8      	415633797	 2.884 ns/op	 0 B/op	  0 allocs/op
BenchmarkNoneStruct/GenericPtr-8   	331620082	 3.580 ns/op	 0 B/op	  0 allocs/op
BenchmarkNoneStruct/GenericSlice-8 	387593746	 3.115 ns/op	 0 B/op	  0 allocs/op
Init + Get (non-empty value)
# int
BenchmarkSomeInt/Typed-8        	499550200	 2.231 ns/op	 0 B/op	  0 allocs/op
BenchmarkSomeInt/Generic-8      	321369986	 3.491 ns/op	 0 B/op	  0 allocs/op
BenchmarkSomeInt/GenericPtr-8   	 64221356	 16.03 ns/op	 8 B/op	  1 allocs/op
BenchmarkSomeInt/GenericSlice-8 	 71858188	 16.53 ns/op	 8 B/op	  1 allocs/op
# string
BenchmarkSomeString/Typed-8        	192472155	 5.840 ns/op	  0 B/op	 0 allocs/op
BenchmarkSomeString/Generic-8      	197161162	 6.471 ns/op	  0 B/op	 0 allocs/op
BenchmarkSomeString/GenericPtr-8   	 16207524	 98.67 ns/op	 16 B/op	 1 allocs/op
BenchmarkSomeString/GenericSlice-8 	 12426998	 100.4 ns/op	 16 B/op	 1 allocs/op
# struct
BenchmarkSomeStruct/Typed-8          	358631294	 3.407 ns/op	  0 B/op	   0 allocs/op
BenchmarkSomeStruct/Generic-8        	241312274	 4.978 ns/op	  0 B/op	   0 allocs/op
BenchmarkSomeStruct/GenericPtr-8     	 32534370	 33.28 ns/op	 24 B/op	 1 allocs/op
BenchmarkSomeStruct/GenericSlice-8   	 34119435	 33.08 ns/op	 24 B/op	 1 allocs/op

At this point we can see already that the alternatives (based on pointer and slice) require allocations while the approach implemented in go-option doesn't.

Now let's check encoding and decoding.

Encode + Decode
# int
BenchmarkEncodeDecodeInt/Typed-8        	46089481	 22.66 ns/op	  0 B/op	 0 allocs/op
BenchmarkEncodeDecodeInt/Generic-8      	10070619	 119.6 ns/op	 32 B/op	 2 allocs/op
BenchmarkEncodeDecodeInt/GenericPtr-8   	20202076	 58.14 ns/op	 16 B/op	 2 allocs/op
BenchmarkEncodeDecodeInt/GenericSlice-8 	17400481	 66.24 ns/op	 24 B/op	 3 allocs/op
# string
BenchmarkEncodeDecodeString/Typed-8        	 6053182	 191.4 ns/op	  8 B/op	 1 allocs/op
BenchmarkEncodeDecodeString/Generic-8      	 1891269	 668.3 ns/op	 56 B/op	 3 allocs/op
BenchmarkEncodeDecodeString/GenericPtr-8   	 1645518	 659.2 ns/op	 56 B/op	 4 allocs/op
BenchmarkEncodeDecodeString/GenericSlice-8 	 1464177	 775.4 ns/op	 72 B/op	 5 allocs/op
# struct
BenchmarkEncodeDecodeStruct/Typed-8        	12816339	 90.85 ns/op	  3 B/op	 1 allocs/op
BenchmarkEncodeDecodeStruct/Generic-8      	 2304001	 532.5 ns/op	 67 B/op	 3 allocs/op
BenchmarkEncodeDecodeStruct/GenericPtr-8   	 2071520	 570.2 ns/op	 75 B/op	 4 allocs/op
BenchmarkEncodeDecodeStruct/GenericSlice-8 	 2007445	 587.4 ns/op	 99 B/op	 5 allocs/op

As it can be seen generic implementation ~3-4 times slower than the typed one. Thus it is recommended to use pre-generated optionals for basic types supplied with go-option (option.Int, option.String etc.).

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Any added in v1.1.0

type Any struct {
	// contains filtered or unexported fields
}

Any represents an optional value of type any. It can either hold a valid any (IsSome == true) or be empty (IsZero == true).

func NoneAny added in v1.1.0

func NoneAny() Any

NoneAny creates an empty optional Any value. The returned Any will have IsSome() == false and IsZero() == true.

Example
opt := option.NoneAny()
if opt.IsZero() {
	fmt.Println("value is absent")
}
Output:

value is absent

func SomeAny added in v1.1.0

func SomeAny(value any) Any

SomeAny creates an optional Any with the given any value. The returned Any will have IsSome() == true and IsZero() == false.

Example
opt := option.SomeAny("hello")
if opt.IsSome() {
	fmt.Println(opt.Unwrap())
}
Output:

hello

func (*Any) DecodeMsgpack added in v1.1.0

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

DecodeMsgpack decodes a Any value from MessagePack format. Supports two input types:

  • nil: interpreted as no value (NoneAny)
  • any: interpreted as a present value (SomeAny)

Returns an error if the input type is unsupported or decoding fails.

After successful decoding:

  • on nil: exists = false, value = default zero value
  • on any: exists = true, value = decoded value

func (Any) EncodeMsgpack added in v1.1.0

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

EncodeMsgpack encodes the Any value using MessagePack format. - If the value is present, it is encoded as any. - If the value is absent (None), it is encoded as nil.

Returns an error if encoding fails.

func (Any) Get added in v1.1.0

func (o Any) Get() (any, 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 any, false).

Recommended usage:

if value, ok := o.Get(); ok {
    // use value
}
Example
some := option.SomeAny("hello")
none := option.NoneAny()
val, ok := some.Get()
fmt.Println(val, ok)
val, ok = none.Get()
fmt.Println(val, ok)
Output:

hello true
<nil> false

func (Any) IsNil added in v1.1.0

func (o Any) IsNil() bool

IsNil is an alias for IsZero.

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

Example
some := option.SomeAny("hello")
none := option.NoneAny()
fmt.Println(some.IsNil() == some.IsZero())
fmt.Println(none.IsNil() == none.IsZero())
Output:

true
true

func (Any) IsSome added in v1.1.0

func (o Any) IsSome() bool

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

Example
some := option.SomeAny("hello")
none := option.NoneAny()
fmt.Println(some.IsSome())
fmt.Println(none.IsSome())
Output:

true
false

func (Any) IsZero added in v1.1.0

func (o Any) IsZero() bool

IsZero returns true if the Any 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.

Example
some := option.SomeAny("hello")
none := option.NoneAny()
fmt.Println(some.IsZero())
fmt.Println(none.IsZero())
Output:

false
true

func (Any) MustGet added in v1.1.0

func (o Any) MustGet() any

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.

Example
some := option.SomeAny("hello")
fmt.Println(some.MustGet())
Output:

hello
Example (Panic)
none := option.NoneAny()
eof := false
defer func() {
	if !eof {
		fmt.Println("panic!", recover())
	}
}()
fmt.Println(none.MustGet())
eof = true
Output:

panic! optional value is not set

func (Any) Unwrap added in v1.1.0

func (o Any) Unwrap() any

Unwrap returns the stored value regardless of presence. If no value is set, returns the zero value for any.

Warning: Does not check presence. Use IsSome() before calling if you need to distinguish between absent value and explicit zero value.

Example
some := option.SomeAny("hello")
none := option.NoneAny()
fmt.Println(some.Unwrap())
fmt.Println(none.Unwrap())
Output:

hello
<nil>

func (Any) UnwrapOr added in v1.1.0

func (o Any) UnwrapOr(defaultValue any) any

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

Example
some := option.SomeAny("hello")
none := option.NoneAny()
fmt.Println(some.UnwrapOr("bye"))
fmt.Println(none.UnwrapOr("bye"))
Output:

hello
bye

func (Any) UnwrapOrElse added in v1.1.0

func (o Any) UnwrapOrElse(defaultValue func() any) any

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
some := option.SomeAny("hello")
none := option.NoneAny()
fmt.Println(some.UnwrapOrElse(func() any {
	return "bye"
}))
fmt.Println(none.UnwrapOrElse(func() any {
	return "bye"
}))
Output:

hello
bye

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
opt := option.NoneBool()
if opt.IsZero() {
	fmt.Println("value is absent")
}
Output:

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
opt := option.SomeBool(true)
if opt.IsSome() {
	fmt.Println(opt.Unwrap())
}
Output:

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
}
Example
some := option.SomeBool(true)
none := option.NoneBool()
val, ok := some.Get()
fmt.Println(val, ok)
val, ok = none.Get()
fmt.Println(val, ok)
Output:

true true
false false

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.

Example
some := option.SomeBool(true)
none := option.NoneBool()
fmt.Println(some.IsNil() == some.IsZero())
fmt.Println(none.IsNil() == none.IsZero())
Output:

true
true

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).

Example
some := option.SomeBool(true)
none := option.NoneBool()
fmt.Println(some.IsSome())
fmt.Println(none.IsSome())
Output:

true
false

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.

Example
some := option.SomeBool(true)
none := option.NoneBool()
fmt.Println(some.IsZero())
fmt.Println(none.IsZero())
Output:

false
true

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.

Example
some := option.SomeBool(true)
fmt.Println(some.MustGet())
Output:

true
Example (Panic)
none := option.NoneBool()
eof := false
defer func() {
	if !eof {
		fmt.Println("panic!", recover())
	}
}()
fmt.Println(none.MustGet())
eof = true
Output:

panic! optional value is not 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.

Example
some := option.SomeBool(true)
none := option.NoneBool()
fmt.Println(some.Unwrap())
fmt.Println(none.Unwrap())
Output:

true
false

func (Bool) UnwrapOr

func (o Bool) UnwrapOr(defaultValue bool) bool

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

Example
some := option.SomeBool(true)
none := option.NoneBool()
fmt.Println(some.UnwrapOr(false))
fmt.Println(none.UnwrapOr(false))
Output:

true
false

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
some := option.SomeBool(true)
none := option.NoneBool()
fmt.Println(some.UnwrapOrElse(func() bool {
	return false
}))
fmt.Println(none.UnwrapOrElse(func() bool {
	return false
}))
Output:

true
false

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
opt := option.NoneByte()
if opt.IsZero() {
	fmt.Println("value is absent")
}
Output:

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
opt := option.SomeByte(12)
if opt.IsSome() {
	fmt.Println(opt.Unwrap())
}
Output:

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
}
Example
some := option.SomeByte(12)
none := option.NoneByte()
val, ok := some.Get()
fmt.Println(val, ok)
val, ok = none.Get()
fmt.Println(val, ok)
Output:

12 true
0 false

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.

Example
some := option.SomeByte(12)
none := option.NoneByte()
fmt.Println(some.IsNil() == some.IsZero())
fmt.Println(none.IsNil() == none.IsZero())
Output:

true
true

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).

Example
some := option.SomeByte(12)
none := option.NoneByte()
fmt.Println(some.IsSome())
fmt.Println(none.IsSome())
Output:

true
false

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.

Example
some := option.SomeByte(12)
none := option.NoneByte()
fmt.Println(some.IsZero())
fmt.Println(none.IsZero())
Output:

false
true

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.

Example
some := option.SomeByte(12)
fmt.Println(some.MustGet())
Output:

12
Example (Panic)
none := option.NoneByte()
eof := false
defer func() {
	if !eof {
		fmt.Println("panic!", recover())
	}
}()
fmt.Println(none.MustGet())
eof = true
Output:

panic! optional value is not 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.

Example
some := option.SomeByte(12)
none := option.NoneByte()
fmt.Println(some.Unwrap())
fmt.Println(none.Unwrap())
Output:

12
0

func (Byte) UnwrapOr

func (o Byte) UnwrapOr(defaultValue byte) byte

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

Example
some := option.SomeByte(12)
none := option.NoneByte()
fmt.Println(some.UnwrapOr(13))
fmt.Println(none.UnwrapOr(13))
Output:

12
13

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
some := option.SomeByte(12)
none := option.NoneByte()
fmt.Println(some.UnwrapOrElse(func() byte {
	return 13
}))
fmt.Println(none.UnwrapOrElse(func() byte {
	return 13
}))
Output:

12
13

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
opt := option.NoneBytes()
if opt.IsZero() {
	fmt.Println("value is absent")
}
Output:

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
opt := option.SomeBytes([]byte{3, 14, 15})
if opt.IsSome() {
	fmt.Println(opt.Unwrap())
}
Output:

[3 14 15]

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
}
Example
some := option.SomeBytes([]byte{3, 14, 15})
none := option.NoneBytes()
val, ok := some.Get()
fmt.Println(val, ok)
val, ok = none.Get()
fmt.Println(val, ok)
Output:

[3 14 15] true
[] false

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.

Example
some := option.SomeBytes([]byte{3, 14, 15})
none := option.NoneBytes()
fmt.Println(some.IsNil() == some.IsZero())
fmt.Println(none.IsNil() == none.IsZero())
Output:

true
true

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).

Example
some := option.SomeBytes([]byte{3, 14, 15})
none := option.NoneBytes()
fmt.Println(some.IsSome())
fmt.Println(none.IsSome())
Output:

true
false

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.

Example
some := option.SomeBytes([]byte{3, 14, 15})
none := option.NoneBytes()
fmt.Println(some.IsZero())
fmt.Println(none.IsZero())
Output:

false
true

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.

Example
some := option.SomeBytes([]byte{3, 14, 15})
fmt.Println(some.MustGet())
Output:

[3 14 15]
Example (Panic)
none := option.NoneBytes()
eof := false
defer func() {
	if !eof {
		fmt.Println("panic!", recover())
	}
}()
fmt.Println(none.MustGet())
eof = true
Output:

panic! optional value is not 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.

Example
some := option.SomeBytes([]byte{3, 14, 15})
none := option.NoneBytes()
fmt.Println(some.Unwrap())
fmt.Println(none.Unwrap())
Output:

[3 14 15]
[]

func (Bytes) UnwrapOr

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

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

Example
some := option.SomeBytes([]byte{3, 14, 15})
none := option.NoneBytes()
fmt.Println(some.UnwrapOr([]byte{3, 14, 15, 9, 26}))
fmt.Println(none.UnwrapOr([]byte{3, 14, 15, 9, 26}))
Output:

[3 14 15]
[3 14 15 9 26]

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
some := option.SomeBytes([]byte{3, 14, 15})
none := option.NoneBytes()
fmt.Println(some.UnwrapOrElse(func() []byte {
	return []byte{3, 14, 15, 9, 26}
}))
fmt.Println(none.UnwrapOrElse(func() []byte {
	return []byte{3, 14, 15, 9, 26}
}))
Output:

[3 14 15]
[3 14 15 9 26]

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
opt := option.NoneFloat32()
if opt.IsZero() {
	fmt.Println("value is absent")
}
Output:

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
opt := option.SomeFloat32(12)
if opt.IsSome() {
	fmt.Println(opt.Unwrap())
}
Output:

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
}
Example
some := option.SomeFloat32(12)
none := option.NoneFloat32()
val, ok := some.Get()
fmt.Println(val, ok)
val, ok = none.Get()
fmt.Println(val, ok)
Output:

12 true
0 false

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.

Example
some := option.SomeFloat32(12)
none := option.NoneFloat32()
fmt.Println(some.IsNil() == some.IsZero())
fmt.Println(none.IsNil() == none.IsZero())
Output:

true
true

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).

Example
some := option.SomeFloat32(12)
none := option.NoneFloat32()
fmt.Println(some.IsSome())
fmt.Println(none.IsSome())
Output:

true
false

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.

Example
some := option.SomeFloat32(12)
none := option.NoneFloat32()
fmt.Println(some.IsZero())
fmt.Println(none.IsZero())
Output:

false
true

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.

Example
some := option.SomeFloat32(12)
fmt.Println(some.MustGet())
Output:

12
Example (Panic)
none := option.NoneFloat32()
eof := false
defer func() {
	if !eof {
		fmt.Println("panic!", recover())
	}
}()
fmt.Println(none.MustGet())
eof = true
Output:

panic! optional value is not 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.

Example
some := option.SomeFloat32(12)
none := option.NoneFloat32()
fmt.Println(some.Unwrap())
fmt.Println(none.Unwrap())
Output:

12
0

func (Float32) UnwrapOr

func (o Float32) UnwrapOr(defaultValue float32) float32

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

Example
some := option.SomeFloat32(12)
none := option.NoneFloat32()
fmt.Println(some.UnwrapOr(13))
fmt.Println(none.UnwrapOr(13))
Output:

12
13

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
some := option.SomeFloat32(12)
none := option.NoneFloat32()
fmt.Println(some.UnwrapOrElse(func() float32 {
	return 13
}))
fmt.Println(none.UnwrapOrElse(func() float32 {
	return 13
}))
Output:

12
13

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
opt := option.NoneFloat64()
if opt.IsZero() {
	fmt.Println("value is absent")
}
Output:

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
opt := option.SomeFloat64(12)
if opt.IsSome() {
	fmt.Println(opt.Unwrap())
}
Output:

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
}
Example
some := option.SomeFloat64(12)
none := option.NoneFloat64()
val, ok := some.Get()
fmt.Println(val, ok)
val, ok = none.Get()
fmt.Println(val, ok)
Output:

12 true
0 false

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.

Example
some := option.SomeFloat64(12)
none := option.NoneFloat64()
fmt.Println(some.IsNil() == some.IsZero())
fmt.Println(none.IsNil() == none.IsZero())
Output:

true
true

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).

Example
some := option.SomeFloat64(12)
none := option.NoneFloat64()
fmt.Println(some.IsSome())
fmt.Println(none.IsSome())
Output:

true
false

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.

Example
some := option.SomeFloat64(12)
none := option.NoneFloat64()
fmt.Println(some.IsZero())
fmt.Println(none.IsZero())
Output:

false
true

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.

Example
some := option.SomeFloat64(12)
fmt.Println(some.MustGet())
Output:

12
Example (Panic)
none := option.NoneFloat64()
eof := false
defer func() {
	if !eof {
		fmt.Println("panic!", recover())
	}
}()
fmt.Println(none.MustGet())
eof = true
Output:

panic! optional value is not 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.

Example
some := option.SomeFloat64(12)
none := option.NoneFloat64()
fmt.Println(some.Unwrap())
fmt.Println(none.Unwrap())
Output:

12
0

func (Float64) UnwrapOr

func (o Float64) UnwrapOr(defaultValue float64) float64

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

Example
some := option.SomeFloat64(12)
none := option.NoneFloat64()
fmt.Println(some.UnwrapOr(13))
fmt.Println(none.UnwrapOr(13))
Output:

12
13

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
some := option.SomeFloat64(12)
none := option.NoneFloat64()
fmt.Println(some.UnwrapOrElse(func() float64 {
	return 13
}))
fmt.Println(none.UnwrapOrElse(func() float64 {
	return 13
}))
Output:

12
13

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.IsSome())
fmt.Println(opt.Unwrap())
Output:

false
0

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())
fmt.Println(opt.Unwrap())
Output:

true
hello

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
some := option.Some(12)
none := option.None[int]()

val, ok := some.Get()
fmt.Println(val, ok)

val, ok = none.Get()
fmt.Println(val, ok)
Output:

12 true
0 false

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.

Example
some := option.Some("hello")
none := option.None[string]()

fmt.Println(some.IsNil() == some.IsZero())
fmt.Println(none.IsNil() == none.IsZero())
Output:

true
true

func (Generic[T]) IsSome

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

IsSome returns true if the optional contains a value.

Example
some := option.Some("hello")
none := option.None[string]()

fmt.Println(some.IsSome())
fmt.Println(none.IsSome())
Output:

true
false

func (Generic[T]) IsZero

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

IsZero returns true if the optional does not contain a value.

Example
some := option.Some("hello")
none := option.None[string]()

fmt.Println(some.IsZero())
fmt.Println(none.IsZero())
Output:

false
true

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
some := option.Some(12)
fmt.Println(some.MustGet())
Output:

12
Example (Panic)
none := option.None[int]()
eof := false

defer func() {
	if !eof {
		fmt.Println("panic!", recover())
	}
}()

fmt.Println(none.MustGet())

eof = true
Output:

panic! optional value is 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.

Example
some := option.Some(12)
none := option.None[int]()

fmt.Println(some.Unwrap())
fmt.Println(none.Unwrap())
Output:

12
0

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
some := option.Some(12)
none := option.None[int]()

fmt.Println(some.UnwrapOr(13))
fmt.Println(none.UnwrapOr(13))
Output:

12
13

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
some := option.Some(12)
none := option.None[int]()

fmt.Println(some.UnwrapOrElse(func() int {
	return 13
}))
fmt.Println(none.UnwrapOrElse(func() int {
	return 13
}))
Output:

12
13

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
opt := option.NoneInt()
if opt.IsZero() {
	fmt.Println("value is absent")
}
Output:

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
opt := option.SomeInt(12)
if opt.IsSome() {
	fmt.Println(opt.Unwrap())
}
Output:

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
}
Example
some := option.SomeInt(12)
none := option.NoneInt()
val, ok := some.Get()
fmt.Println(val, ok)
val, ok = none.Get()
fmt.Println(val, ok)
Output:

12 true
0 false

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.

Example
some := option.SomeInt(12)
none := option.NoneInt()
fmt.Println(some.IsNil() == some.IsZero())
fmt.Println(none.IsNil() == none.IsZero())
Output:

true
true

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).

Example
some := option.SomeInt(12)
none := option.NoneInt()
fmt.Println(some.IsSome())
fmt.Println(none.IsSome())
Output:

true
false

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.

Example
some := option.SomeInt(12)
none := option.NoneInt()
fmt.Println(some.IsZero())
fmt.Println(none.IsZero())
Output:

false
true

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.

Example
some := option.SomeInt(12)
fmt.Println(some.MustGet())
Output:

12
Example (Panic)
none := option.NoneInt()
eof := false
defer func() {
	if !eof {
		fmt.Println("panic!", recover())
	}
}()
fmt.Println(none.MustGet())
eof = true
Output:

panic! optional value is not 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.

Example
some := option.SomeInt(12)
none := option.NoneInt()
fmt.Println(some.Unwrap())
fmt.Println(none.Unwrap())
Output:

12
0

func (Int) UnwrapOr

func (o Int) UnwrapOr(defaultValue int) int

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

Example
some := option.SomeInt(12)
none := option.NoneInt()
fmt.Println(some.UnwrapOr(13))
fmt.Println(none.UnwrapOr(13))
Output:

12
13

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
some := option.SomeInt(12)
none := option.NoneInt()
fmt.Println(some.UnwrapOrElse(func() int {
	return 13
}))
fmt.Println(none.UnwrapOrElse(func() int {
	return 13
}))
Output:

12
13

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
opt := option.NoneInt16()
if opt.IsZero() {
	fmt.Println("value is absent")
}
Output:

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
opt := option.SomeInt16(12)
if opt.IsSome() {
	fmt.Println(opt.Unwrap())
}
Output:

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
}
Example
some := option.SomeInt16(12)
none := option.NoneInt16()
val, ok := some.Get()
fmt.Println(val, ok)
val, ok = none.Get()
fmt.Println(val, ok)
Output:

12 true
0 false

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.

Example
some := option.SomeInt16(12)
none := option.NoneInt16()
fmt.Println(some.IsNil() == some.IsZero())
fmt.Println(none.IsNil() == none.IsZero())
Output:

true
true

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).

Example
some := option.SomeInt16(12)
none := option.NoneInt16()
fmt.Println(some.IsSome())
fmt.Println(none.IsSome())
Output:

true
false

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.

Example
some := option.SomeInt16(12)
none := option.NoneInt16()
fmt.Println(some.IsZero())
fmt.Println(none.IsZero())
Output:

false
true

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.

Example
some := option.SomeInt16(12)
fmt.Println(some.MustGet())
Output:

12
Example (Panic)
none := option.NoneInt16()
eof := false
defer func() {
	if !eof {
		fmt.Println("panic!", recover())
	}
}()
fmt.Println(none.MustGet())
eof = true
Output:

panic! optional value is not 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.

Example
some := option.SomeInt16(12)
none := option.NoneInt16()
fmt.Println(some.Unwrap())
fmt.Println(none.Unwrap())
Output:

12
0

func (Int16) UnwrapOr

func (o Int16) UnwrapOr(defaultValue int16) int16

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

Example
some := option.SomeInt16(12)
none := option.NoneInt16()
fmt.Println(some.UnwrapOr(13))
fmt.Println(none.UnwrapOr(13))
Output:

12
13

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
some := option.SomeInt16(12)
none := option.NoneInt16()
fmt.Println(some.UnwrapOrElse(func() int16 {
	return 13
}))
fmt.Println(none.UnwrapOrElse(func() int16 {
	return 13
}))
Output:

12
13

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
opt := option.NoneInt32()
if opt.IsZero() {
	fmt.Println("value is absent")
}
Output:

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
opt := option.SomeInt32(12)
if opt.IsSome() {
	fmt.Println(opt.Unwrap())
}
Output:

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
}
Example
some := option.SomeInt32(12)
none := option.NoneInt32()
val, ok := some.Get()
fmt.Println(val, ok)
val, ok = none.Get()
fmt.Println(val, ok)
Output:

12 true
0 false

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.

Example
some := option.SomeInt32(12)
none := option.NoneInt32()
fmt.Println(some.IsNil() == some.IsZero())
fmt.Println(none.IsNil() == none.IsZero())
Output:

true
true

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).

Example
some := option.SomeInt32(12)
none := option.NoneInt32()
fmt.Println(some.IsSome())
fmt.Println(none.IsSome())
Output:

true
false

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.

Example
some := option.SomeInt32(12)
none := option.NoneInt32()
fmt.Println(some.IsZero())
fmt.Println(none.IsZero())
Output:

false
true

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.

Example
some := option.SomeInt32(12)
fmt.Println(some.MustGet())
Output:

12
Example (Panic)
none := option.NoneInt32()
eof := false
defer func() {
	if !eof {
		fmt.Println("panic!", recover())
	}
}()
fmt.Println(none.MustGet())
eof = true
Output:

panic! optional value is not 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.

Example
some := option.SomeInt32(12)
none := option.NoneInt32()
fmt.Println(some.Unwrap())
fmt.Println(none.Unwrap())
Output:

12
0

func (Int32) UnwrapOr

func (o Int32) UnwrapOr(defaultValue int32) int32

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

Example
some := option.SomeInt32(12)
none := option.NoneInt32()
fmt.Println(some.UnwrapOr(13))
fmt.Println(none.UnwrapOr(13))
Output:

12
13

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
some := option.SomeInt32(12)
none := option.NoneInt32()
fmt.Println(some.UnwrapOrElse(func() int32 {
	return 13
}))
fmt.Println(none.UnwrapOrElse(func() int32 {
	return 13
}))
Output:

12
13

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
opt := option.NoneInt64()
if opt.IsZero() {
	fmt.Println("value is absent")
}
Output:

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
opt := option.SomeInt64(12)
if opt.IsSome() {
	fmt.Println(opt.Unwrap())
}
Output:

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
}
Example
some := option.SomeInt64(12)
none := option.NoneInt64()
val, ok := some.Get()
fmt.Println(val, ok)
val, ok = none.Get()
fmt.Println(val, ok)
Output:

12 true
0 false

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.

Example
some := option.SomeInt64(12)
none := option.NoneInt64()
fmt.Println(some.IsNil() == some.IsZero())
fmt.Println(none.IsNil() == none.IsZero())
Output:

true
true

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).

Example
some := option.SomeInt64(12)
none := option.NoneInt64()
fmt.Println(some.IsSome())
fmt.Println(none.IsSome())
Output:

true
false

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.

Example
some := option.SomeInt64(12)
none := option.NoneInt64()
fmt.Println(some.IsZero())
fmt.Println(none.IsZero())
Output:

false
true

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.

Example
some := option.SomeInt64(12)
fmt.Println(some.MustGet())
Output:

12
Example (Panic)
none := option.NoneInt64()
eof := false
defer func() {
	if !eof {
		fmt.Println("panic!", recover())
	}
}()
fmt.Println(none.MustGet())
eof = true
Output:

panic! optional value is not 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.

Example
some := option.SomeInt64(12)
none := option.NoneInt64()
fmt.Println(some.Unwrap())
fmt.Println(none.Unwrap())
Output:

12
0

func (Int64) UnwrapOr

func (o Int64) UnwrapOr(defaultValue int64) int64

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

Example
some := option.SomeInt64(12)
none := option.NoneInt64()
fmt.Println(some.UnwrapOr(13))
fmt.Println(none.UnwrapOr(13))
Output:

12
13

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
some := option.SomeInt64(12)
none := option.NoneInt64()
fmt.Println(some.UnwrapOrElse(func() int64 {
	return 13
}))
fmt.Println(none.UnwrapOrElse(func() int64 {
	return 13
}))
Output:

12
13

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
opt := option.NoneInt8()
if opt.IsZero() {
	fmt.Println("value is absent")
}
Output:

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
opt := option.SomeInt8(12)
if opt.IsSome() {
	fmt.Println(opt.Unwrap())
}
Output:

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
}
Example
some := option.SomeInt8(12)
none := option.NoneInt8()
val, ok := some.Get()
fmt.Println(val, ok)
val, ok = none.Get()
fmt.Println(val, ok)
Output:

12 true
0 false

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.

Example
some := option.SomeInt8(12)
none := option.NoneInt8()
fmt.Println(some.IsNil() == some.IsZero())
fmt.Println(none.IsNil() == none.IsZero())
Output:

true
true

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).

Example
some := option.SomeInt8(12)
none := option.NoneInt8()
fmt.Println(some.IsSome())
fmt.Println(none.IsSome())
Output:

true
false

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.

Example
some := option.SomeInt8(12)
none := option.NoneInt8()
fmt.Println(some.IsZero())
fmt.Println(none.IsZero())
Output:

false
true

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.

Example
some := option.SomeInt8(12)
fmt.Println(some.MustGet())
Output:

12
Example (Panic)
none := option.NoneInt8()
eof := false
defer func() {
	if !eof {
		fmt.Println("panic!", recover())
	}
}()
fmt.Println(none.MustGet())
eof = true
Output:

panic! optional value is not 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.

Example
some := option.SomeInt8(12)
none := option.NoneInt8()
fmt.Println(some.Unwrap())
fmt.Println(none.Unwrap())
Output:

12
0

func (Int8) UnwrapOr

func (o Int8) UnwrapOr(defaultValue int8) int8

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

Example
some := option.SomeInt8(12)
none := option.NoneInt8()
fmt.Println(some.UnwrapOr(13))
fmt.Println(none.UnwrapOr(13))
Output:

12
13

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
some := option.SomeInt8(12)
none := option.NoneInt8()
fmt.Println(some.UnwrapOrElse(func() int8 {
	return 13
}))
fmt.Println(none.UnwrapOrElse(func() int8 {
	return 13
}))
Output:

12
13

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
opt := option.NoneString()
if opt.IsZero() {
	fmt.Println("value is absent")
}
Output:

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
opt := option.SomeString("hello")
if opt.IsSome() {
	fmt.Println(opt.Unwrap())
}
Output:

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
}
Example
some := option.SomeString("hello")
none := option.NoneString()
val, ok := some.Get()
fmt.Println(val, ok)
val, ok = none.Get()
fmt.Println(val, ok)
Output:

hello true
 false

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.

Example
some := option.SomeString("hello")
none := option.NoneString()
fmt.Println(some.IsNil() == some.IsZero())
fmt.Println(none.IsNil() == none.IsZero())
Output:

true
true

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).

Example
some := option.SomeString("hello")
none := option.NoneString()
fmt.Println(some.IsSome())
fmt.Println(none.IsSome())
Output:

true
false

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.

Example
some := option.SomeString("hello")
none := option.NoneString()
fmt.Println(some.IsZero())
fmt.Println(none.IsZero())
Output:

false
true

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.

Example
some := option.SomeString("hello")
fmt.Println(some.MustGet())
Output:

hello
Example (Panic)
none := option.NoneString()
eof := false
defer func() {
	if !eof {
		fmt.Println("panic!", recover())
	}
}()
fmt.Println(none.MustGet())
eof = true
Output:

panic! optional value is not 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.

Example
some := option.SomeString("hello")
none := option.NoneString()
fmt.Println(some.Unwrap())
fmt.Println(none.Unwrap())
Output:

hello

func (String) UnwrapOr

func (o String) UnwrapOr(defaultValue string) string

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

Example
some := option.SomeString("hello")
none := option.NoneString()
fmt.Println(some.UnwrapOr("bye"))
fmt.Println(none.UnwrapOr("bye"))
Output:

hello
bye

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
some := option.SomeString("hello")
none := option.NoneString()
fmt.Println(some.UnwrapOrElse(func() string {
	return "bye"
}))
fmt.Println(none.UnwrapOrElse(func() string {
	return "bye"
}))
Output:

hello
bye

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
opt := option.NoneUint()
if opt.IsZero() {
	fmt.Println("value is absent")
}
Output:

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
opt := option.SomeUint(12)
if opt.IsSome() {
	fmt.Println(opt.Unwrap())
}
Output:

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
}
Example
some := option.SomeUint(12)
none := option.NoneUint()
val, ok := some.Get()
fmt.Println(val, ok)
val, ok = none.Get()
fmt.Println(val, ok)
Output:

12 true
0 false

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.

Example
some := option.SomeUint(12)
none := option.NoneUint()
fmt.Println(some.IsNil() == some.IsZero())
fmt.Println(none.IsNil() == none.IsZero())
Output:

true
true

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).

Example
some := option.SomeUint(12)
none := option.NoneUint()
fmt.Println(some.IsSome())
fmt.Println(none.IsSome())
Output:

true
false

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.

Example
some := option.SomeUint(12)
none := option.NoneUint()
fmt.Println(some.IsZero())
fmt.Println(none.IsZero())
Output:

false
true

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.

Example
some := option.SomeUint(12)
fmt.Println(some.MustGet())
Output:

12
Example (Panic)
none := option.NoneUint()
eof := false
defer func() {
	if !eof {
		fmt.Println("panic!", recover())
	}
}()
fmt.Println(none.MustGet())
eof = true
Output:

panic! optional value is not 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.

Example
some := option.SomeUint(12)
none := option.NoneUint()
fmt.Println(some.Unwrap())
fmt.Println(none.Unwrap())
Output:

12
0

func (Uint) UnwrapOr

func (o Uint) UnwrapOr(defaultValue uint) uint

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

Example
some := option.SomeUint(12)
none := option.NoneUint()
fmt.Println(some.UnwrapOr(13))
fmt.Println(none.UnwrapOr(13))
Output:

12
13

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
some := option.SomeUint(12)
none := option.NoneUint()
fmt.Println(some.UnwrapOrElse(func() uint {
	return 13
}))
fmt.Println(none.UnwrapOrElse(func() uint {
	return 13
}))
Output:

12
13

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
opt := option.NoneUint16()
if opt.IsZero() {
	fmt.Println("value is absent")
}
Output:

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
opt := option.SomeUint16(12)
if opt.IsSome() {
	fmt.Println(opt.Unwrap())
}
Output:

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
}
Example
some := option.SomeUint16(12)
none := option.NoneUint16()
val, ok := some.Get()
fmt.Println(val, ok)
val, ok = none.Get()
fmt.Println(val, ok)
Output:

12 true
0 false

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.

Example
some := option.SomeUint16(12)
none := option.NoneUint16()
fmt.Println(some.IsNil() == some.IsZero())
fmt.Println(none.IsNil() == none.IsZero())
Output:

true
true

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).

Example
some := option.SomeUint16(12)
none := option.NoneUint16()
fmt.Println(some.IsSome())
fmt.Println(none.IsSome())
Output:

true
false

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.

Example
some := option.SomeUint16(12)
none := option.NoneUint16()
fmt.Println(some.IsZero())
fmt.Println(none.IsZero())
Output:

false
true

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.

Example
some := option.SomeUint16(12)
fmt.Println(some.MustGet())
Output:

12
Example (Panic)
none := option.NoneUint16()
eof := false
defer func() {
	if !eof {
		fmt.Println("panic!", recover())
	}
}()
fmt.Println(none.MustGet())
eof = true
Output:

panic! optional value is not 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.

Example
some := option.SomeUint16(12)
none := option.NoneUint16()
fmt.Println(some.Unwrap())
fmt.Println(none.Unwrap())
Output:

12
0

func (Uint16) UnwrapOr

func (o Uint16) UnwrapOr(defaultValue uint16) uint16

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

Example
some := option.SomeUint16(12)
none := option.NoneUint16()
fmt.Println(some.UnwrapOr(13))
fmt.Println(none.UnwrapOr(13))
Output:

12
13

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
some := option.SomeUint16(12)
none := option.NoneUint16()
fmt.Println(some.UnwrapOrElse(func() uint16 {
	return 13
}))
fmt.Println(none.UnwrapOrElse(func() uint16 {
	return 13
}))
Output:

12
13

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
opt := option.NoneUint32()
if opt.IsZero() {
	fmt.Println("value is absent")
}
Output:

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
opt := option.SomeUint32(12)
if opt.IsSome() {
	fmt.Println(opt.Unwrap())
}
Output:

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
}
Example
some := option.SomeUint32(12)
none := option.NoneUint32()
val, ok := some.Get()
fmt.Println(val, ok)
val, ok = none.Get()
fmt.Println(val, ok)
Output:

12 true
0 false

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.

Example
some := option.SomeUint32(12)
none := option.NoneUint32()
fmt.Println(some.IsNil() == some.IsZero())
fmt.Println(none.IsNil() == none.IsZero())
Output:

true
true

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).

Example
some := option.SomeUint32(12)
none := option.NoneUint32()
fmt.Println(some.IsSome())
fmt.Println(none.IsSome())
Output:

true
false

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.

Example
some := option.SomeUint32(12)
none := option.NoneUint32()
fmt.Println(some.IsZero())
fmt.Println(none.IsZero())
Output:

false
true

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.

Example
some := option.SomeUint32(12)
fmt.Println(some.MustGet())
Output:

12
Example (Panic)
none := option.NoneUint32()
eof := false
defer func() {
	if !eof {
		fmt.Println("panic!", recover())
	}
}()
fmt.Println(none.MustGet())
eof = true
Output:

panic! optional value is not 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.

Example
some := option.SomeUint32(12)
none := option.NoneUint32()
fmt.Println(some.Unwrap())
fmt.Println(none.Unwrap())
Output:

12
0

func (Uint32) UnwrapOr

func (o Uint32) UnwrapOr(defaultValue uint32) uint32

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

Example
some := option.SomeUint32(12)
none := option.NoneUint32()
fmt.Println(some.UnwrapOr(13))
fmt.Println(none.UnwrapOr(13))
Output:

12
13

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
some := option.SomeUint32(12)
none := option.NoneUint32()
fmt.Println(some.UnwrapOrElse(func() uint32 {
	return 13
}))
fmt.Println(none.UnwrapOrElse(func() uint32 {
	return 13
}))
Output:

12
13

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
opt := option.NoneUint64()
if opt.IsZero() {
	fmt.Println("value is absent")
}
Output:

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
opt := option.SomeUint64(12)
if opt.IsSome() {
	fmt.Println(opt.Unwrap())
}
Output:

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
}
Example
some := option.SomeUint64(12)
none := option.NoneUint64()
val, ok := some.Get()
fmt.Println(val, ok)
val, ok = none.Get()
fmt.Println(val, ok)
Output:

12 true
0 false

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.

Example
some := option.SomeUint64(12)
none := option.NoneUint64()
fmt.Println(some.IsNil() == some.IsZero())
fmt.Println(none.IsNil() == none.IsZero())
Output:

true
true

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).

Example
some := option.SomeUint64(12)
none := option.NoneUint64()
fmt.Println(some.IsSome())
fmt.Println(none.IsSome())
Output:

true
false

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.

Example
some := option.SomeUint64(12)
none := option.NoneUint64()
fmt.Println(some.IsZero())
fmt.Println(none.IsZero())
Output:

false
true

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.

Example
some := option.SomeUint64(12)
fmt.Println(some.MustGet())
Output:

12
Example (Panic)
none := option.NoneUint64()
eof := false
defer func() {
	if !eof {
		fmt.Println("panic!", recover())
	}
}()
fmt.Println(none.MustGet())
eof = true
Output:

panic! optional value is not 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.

Example
some := option.SomeUint64(12)
none := option.NoneUint64()
fmt.Println(some.Unwrap())
fmt.Println(none.Unwrap())
Output:

12
0

func (Uint64) UnwrapOr

func (o Uint64) UnwrapOr(defaultValue uint64) uint64

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

Example
some := option.SomeUint64(12)
none := option.NoneUint64()
fmt.Println(some.UnwrapOr(13))
fmt.Println(none.UnwrapOr(13))
Output:

12
13

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
some := option.SomeUint64(12)
none := option.NoneUint64()
fmt.Println(some.UnwrapOrElse(func() uint64 {
	return 13
}))
fmt.Println(none.UnwrapOrElse(func() uint64 {
	return 13
}))
Output:

12
13

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
opt := option.NoneUint8()
if opt.IsZero() {
	fmt.Println("value is absent")
}
Output:

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
opt := option.SomeUint8(12)
if opt.IsSome() {
	fmt.Println(opt.Unwrap())
}
Output:

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
}
Example
some := option.SomeUint8(12)
none := option.NoneUint8()
val, ok := some.Get()
fmt.Println(val, ok)
val, ok = none.Get()
fmt.Println(val, ok)
Output:

12 true
0 false

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.

Example
some := option.SomeUint8(12)
none := option.NoneUint8()
fmt.Println(some.IsNil() == some.IsZero())
fmt.Println(none.IsNil() == none.IsZero())
Output:

true
true

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).

Example
some := option.SomeUint8(12)
none := option.NoneUint8()
fmt.Println(some.IsSome())
fmt.Println(none.IsSome())
Output:

true
false

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.

Example
some := option.SomeUint8(12)
none := option.NoneUint8()
fmt.Println(some.IsZero())
fmt.Println(none.IsZero())
Output:

false
true

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.

Example
some := option.SomeUint8(12)
fmt.Println(some.MustGet())
Output:

12
Example (Panic)
none := option.NoneUint8()
eof := false
defer func() {
	if !eof {
		fmt.Println("panic!", recover())
	}
}()
fmt.Println(none.MustGet())
eof = true
Output:

panic! optional value is not 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.

Example
some := option.SomeUint8(12)
none := option.NoneUint8()
fmt.Println(some.Unwrap())
fmt.Println(none.Unwrap())
Output:

12
0

func (Uint8) UnwrapOr

func (o Uint8) UnwrapOr(defaultValue uint8) uint8

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

Example
some := option.SomeUint8(12)
none := option.NoneUint8()
fmt.Println(some.UnwrapOr(13))
fmt.Println(none.UnwrapOr(13))
Output:

12
13

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
some := option.SomeUint8(12)
none := option.NoneUint8()
fmt.Println(some.UnwrapOrElse(func() uint8 {
	return 13
}))
fmt.Println(none.UnwrapOrElse(func() uint8 {
	return 13
}))
Output:

12
13

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