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 ¶
- func Contains[T comparable](option Option[T], matches T) (_ bool)
- func Equal[T comparable](a, b Option[T]) (_ bool)
- type Option
- func (Option[T]) Contains(predicate func(v T) bool) (_ bool)
- func (Option[T]) Get() (val T, ok bool)
- func (Option[T]) GetOrElse(alternative T) (_ T)
- func (Option[T]) GetOrElseF(alternative func() T) (_ T)
- func (Option[T]) GoString() (_ string)
- func (Option[T]) IsNone() (_ bool)
- func (Option[T]) IsSome() (_ bool)
- func (Option[T]) IsZero() (_ bool)
- func (Option[T]) MarshalJSON() (_ []byte, _ error)
- func (Option[T]) MustGet() (_ T)
- func (Option[T]) OrElse(alternative T) (_ Option[T])
- func (Option[T]) PtrOrNil() (_ *T)
- func (Option[T]) String() (_ string)
- func (*Option[T]) UnmarshalJSON(data []byte) (_ error)
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 ¶
FromPointer returns Some(*v) if v is not nil, and None otherwise.
func Map ¶
Map returns an Option with the value mapped by the given function if present, otherwise returns None.
func (Option[T]) Get ¶
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]) IsZero ¶
IsZero is an alias for IsNone, to support usage in structs with "omitempty".
func (Option[T]) MarshalJSON ¶
func (Option[T]) MustGet ¶
func (Option[T]) MustGet() (_ T)
MustGet returns the value if present, and otherwise panics.
func (Option[T]) OrElse ¶
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.