Documentation ¶
Index ¶
- func Contains[T comparable](option Option[T], matches T) bool
- func Fold[T, R any](option Option[T], defaultValue R, f func(T) R) R
- func FoldLeft[T, R any](option Option[T], zero R, f func(accum R, value T) R) R
- type Option
- func AsOptional[T comparable](v T) Option[T]
- func CommaOk[T any](v T, ok bool) Option[T]
- func FlatMap[T, R any](option Option[T], f func(T) Option[R]) Option[R]
- func FromPointer[T any](v *T) Option[T]
- func Map[T, R any](option Option[T], f func(T) R) Option[R]
- func None[T any]() Option[T]
- func Some[T any](v T) Option[T]
- func (o Option[T]) Contains(predicate func(v T) bool) bool
- func (o Option[T]) Empty() bool
- func (o Option[T]) Equal(other Option[T]) bool
- func (o Option[T]) ForAll(f func(v T))
- func (o Option[T]) ForEach(predicate func(v T) bool) bool
- func (o Option[T]) Get() (val T, ok bool)
- func (o Option[T]) GetOrElse(alternative T) T
- func (o Option[T]) GetOrElseF(alternative func() T) T
- func (o Option[T]) MustGet() (rtn T)
- func (o Option[T]) OrElse(alternative T) Option[T]
- func (o Option[T]) Present() bool
- func (o Option[T]) PtrOrNil() *T
- func (o Option[T]) String() string
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
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
It is different a normal value as it can distinguish between a zero value and a missing value even on pointer types
func AsOptional ¶
func AsOptional[T comparable](v T) Option[T]
AsOptional returns an Option where a zero value T is considered None and any other value is considered Some
i.e.
AsOptional(nil) == None() AsOptional(0) == None() AsOptional(false) == None() AsOptional("") == None() AsOptional(&MyStruct{}) == Some(&MyStruct{}) AsOptional(1) == Some(1) AsOptional(true) == Some(true)
func CommaOk ¶
CommaOk is a helper function to convert a comma ok idiom into an Option. If ok is true it returns Some(v), otherwise it returns None.
func FlatMap ¶
FlatMap returns an Option with the value mapped by the given function if present, otherwise returns None
func FromPointer ¶ added in v1.21.1
FromPointer returns an Option where a nil pointer is considered None and any other value is considered Some, with the value dereferenced.
func Map ¶
Map returns an Option with the value mapped by the given function if present, otherwise returns None
func Some ¶
Some returns an Option with the given value and present set to true
This means Some(nil) is a valid present Option and Some(nil) != None()
func (Option[T]) Contains ¶
Contains returns true if the Option is present and the given predicate returns true on the value otherwise returns false
func (Option[T]) Equal ¶
Equal reports whether two fields are equal. It's implemented for testing purposes.
func (Option[T]) ForAll ¶
func (o Option[T]) ForAll(f func(v T))
ForAll calls the given function with the value if present
func (Option[T]) ForEach ¶
ForEach returns true if the Option is empty or the given predicate returns true on the value
func (Option[T]) GetOrElse ¶
func (o Option[T]) GetOrElse(alternative T) T
GetOrElse returns the value if present, otherwise returns the alternative value
func (Option[T]) GetOrElseF ¶
func (o Option[T]) GetOrElseF(alternative func() T) T
GetOrElseF returns the value if present, otherwise returns the alternative value
func (Option[T]) MustGet ¶
func (o Option[T]) MustGet() (rtn T)
MustGet returns the value if present, otherwise panics
func (Option[T]) OrElse ¶
OrElse returns an Option with the value if present, otherwise returns the alternative value