optional

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option[T any] struct {
	// contains filtered or unexported fields
}

Option represents an optional value of type T. It can either be empty (None) or contain a value (Some). When marshaled to JSON, an empty Option becomes `null`.

func None

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

None creates an empty Option of type T.

func Some

func Some[T any](v T) Option[T]

Some creates an Option containing the given value.

func (Option[T]) IsNone

func (o Option[T]) IsNone() bool

IsNone returns true if the Option is empty.

func (Option[T]) IsSome

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

IsSome returns true if the Option contains a value.

func (Option[T]) MarshalJSON

func (o Option[T]) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface. An empty Option marshals to literal "null".

func (*Option[T]) UnmarshalJSON

func (o *Option[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface. It correctly handles JSON "null" strings.

func (Option[T]) Unwrap

func (o Option[T]) Unwrap() T

Unwrap returns the inner value. It panics if the Option is empty.

func (Option[T]) UnwrapOr

func (o Option[T]) UnwrapOr(defaultValue T) T

UnwrapOr returns the inner value if present, otherwise returns the provided default value.

Jump to

Keyboard shortcuts

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