option

package
v1.52.1 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2025 License: MPL-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package option provides a generic Option type for representing optional values in a more type-safe way than using pointers or zero values.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains

func Contains[T comparable](option Option[T], matches T) (_ bool)

Contains returns true if the option is present and matches the given value.

func Equal

func Equal[T comparable](a, b Option[T]) (_ bool)

Equal reports whether a and b are equal, using ==. If both are None, they are considered equal.

Types

type Option

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

Option is a type that represents a value that may or may not be present.

func FromComparable

func FromComparable[T comparable](v T) (_ Option[T])

FromComparable returns Some(v) if v is not zero, and None otherwise. If T implements an IsZero() bool method, that is also used to determine if v is zero.

func FromPointer

func FromPointer[T any](v *T) (_ Option[T])

FromPointer returns Some(*v) if v is not nil, and None otherwise.

func Map

func Map[T, R any](option Option[T], f func(T) R) (_ Option[R])

Map returns an Option with the value mapped by the given function if present, otherwise returns None.

func None

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

None returns an Option with no value set.

func Some

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

Some returns an Option with the given value and present set to true.

func (Option[T]) Contains

func (Option[T]) Contains(predicate func(v T) bool) (_ bool)

Contains returns pred(v) if the option contains v, and false otherwise.

func (Option[T]) Get

func (Option[T]) Get() (val T, ok bool)

Get gets the option value and returns ok==true if present. Commonly used in the "comma ok" idiom:

if val, ok := option.Get(); ok {
    ...
}

func (Option[T]) GetOrElse

func (Option[T]) GetOrElse(alternative T) (_ T)

GetOrElse returns the value if present, otherwise returns alternative.

func (Option[T]) GetOrElseF

func (Option[T]) GetOrElseF(alternative func() T) (_ T)

GetOrElseF returns the value if present, otherwise returns alternative().

func (Option[T]) GoString

func (Option[T]) GoString() (_ string)

func (Option[T]) IsNone

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

IsNone returns true if the Option has no value set.

func (Option[T]) IsSome

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

IsSome returns true if the Option has a value set.

func (Option[T]) IsZero

func (Option[T]) IsZero() (_ bool)

IsZero is an alias for IsNone, to support usage in structs with "omitempty".

func (Option[T]) MarshalJSON

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

func (Option[T]) MustGet

func (Option[T]) MustGet() (_ T)

MustGet returns the value if present, and otherwise panics.

func (Option[T]) OrElse

func (Option[T]) OrElse(alternative T) (_ Option[T])

OrElse returns an Option with the value if present, otherwise returns Some(alternative).

func (Option[T]) PtrOrNil

func (Option[T]) PtrOrNil() (_ *T)

PtrOrNil returns the value as a pointer if present, or nil otherwise.

func (Option[T]) String

func (Option[T]) String() (_ string)

func (*Option[T]) UnmarshalJSON

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

Jump to

Keyboard shortcuts

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