Documentation
¶
Overview ¶
Package option provides types and functions to work with optional values.
Index ¶
- Variables
- func Lift[T any](fn func(T)) func(Option[T])
- type Any
- type Bool
- type Byte
- type Error
- type Int
- type Option
- func Lookup[K comparable, V any](m map[K]V, key K) (_ Option[V])
- func Map[T, R any](b Option[T], fn func(T) R) (_ Option[R])
- func New[T any](t T, ok bool) (_ Option[T])
- func NonEmptyWith[R any](s string, fn func(string) R) (_ Option[R])
- func NonNil[T any](t *T) (_ Option[T])
- func NonNilWith[T any, R any](t *T, fn func(T) R) (_ Option[R])
- func NonZero[T comparable](t T) (_ Option[T])
- func NonZeroWith[T comparable, R any](t T, fn func(T) R) (_ Option[R])
- func NotOk[T any]() (_ Option[T])
- func Of[T any](t T) Option[T]
- func (b Option[T]) Convert(fn func(T) T) (_ Option[T])
- func (b Option[T]) Get() (_ T, _ bool)
- func (b Option[T]) IfNotOk(fn func())
- func (b Option[T]) IfOk(fn func(T))
- func (b Option[T]) IsOk() bool
- func (b Option[T]) KeepIf(fn func(T) bool) (_ Option[T])
- func (o Option[T]) MarshalJSON() ([]byte, error)
- func (b Option[T]) MustGet() T
- func (b Option[T]) Or(t T) T
- func (b Option[T]) OrCall(fn func() T) (_ T)
- func (b Option[T]) OrEmpty() (_ T)
- func (b Option[T]) OrFalse() bool
- func (b Option[T]) OrZero() (_ T)
- func (b Option[T]) RemoveIf(fn func(T) bool) (_ Option[T])
- func (b Option[T]) ToAny(fn func(T) any) (_ Option[any])
- func (b Option[T]) ToBool(fn func(T) bool) (_ Option[bool])
- func (b Option[T]) ToByte(fn func(T) byte) (_ Option[byte])
- func (b Option[T]) ToError(fn func(T) error) (_ Option[error])
- func (b Option[T]) ToInt(fn func(T) int) (_ Option[int])
- func (b Option[T]) ToOpt() (_ *T)
- func (b Option[T]) ToRune(fn func(T) rune) (_ Option[rune])
- func (b Option[T]) ToString(fn func(T) string) (_ Option[string])
- func (o *Option[T]) UnmarshalJSON(data []byte) error
- type Rune
- type String
Constants ¶
This section is empty.
Variables ¶
Functions ¶
Types ¶
type Option ¶ added in v0.30.0
type Option[T any] struct { // contains filtered or unexported fields }
Option represents an optional value of type T.
func Lookup ¶ added in v0.23.0
func Lookup[K comparable, V any](m map[K]V, key K) (_ Option[V])
Lookup returns an ok option of the value at key in m, or not-ok if the key is absent.
func NonEmptyWith ¶ added in v0.35.0
NonEmptyWith returns an ok option of fn(s) provided that s is not empty, or not-ok otherwise. It is the string-specific variant of NonZeroWith.
func NonNil ¶ added in v0.30.0
NonNil returns an ok option of *what t points at* provided that t is not nil, or not-ok otherwise. It converts a pointer-based pseudo-option (where nil means absent) into a formal option.
func NonNilWith ¶ added in v0.35.0
NonNilWith returns an ok option of fn(*t) provided that t is not nil, or not-ok otherwise. It dereferences the pointer before passing to fn, matching NonNil's behavior.
func NonZero ¶ added in v0.30.0
func NonZero[T comparable](t T) (_ Option[T])
NonZero returns an ok option of t provided that t is not the zero value for T, or not-ok otherwise. Zero values include "" for strings, 0 for numbers, false for bools, etc.
func NonZeroWith ¶ added in v0.35.0
func NonZeroWith[T comparable, R any](t T, fn func(T) R) (_ Option[R])
NonZeroWith returns an ok option of fn(t) provided that t is not the zero value for T, or not-ok otherwise. It combines NonZero and a transform in one call — check presence and transform in a single step.
func (Option[T]) Convert ¶ added in v0.30.0
Convert returns the result of applying fn to the option's value provided that the option is ok, or not-ok otherwise.
func (Option[T]) Get ¶ added in v0.30.0
Get returns the option's value and a boolean indicating the option's status. It unpacks the option's fields into Go's comma-ok idiom, making it useful in the usual Go conditional constructs. When used in this manner, myVal doesn't stick around in the namespace when you're done with it:
if myVal, ok := o.Get; ok {
do some stuff
}
func (Option[T]) IfNotOk ¶ added in v0.30.0
func (b Option[T]) IfNotOk(fn func())
IfNotOk calls fn if the option is not ok.
func (Option[T]) IfOk ¶ added in v0.30.0
func (b Option[T]) IfOk(fn func(T))
IfOk applies fn to the option's value provided that the option is ok.
func (Option[T]) KeepIf ¶ added in v0.30.0
KeepIf returns b provided that applying fn to an ok option's value returns true, or the original option otherwise. It is the filter operation. Since Go doesn't offer a convenient lambda syntax for constructing the negation of a function's output, there is a RemoveIf method as well.
func (Option[T]) MarshalJSON ¶ added in v0.30.0
MarshalJSON serializes Option: Ok(v) → v, NotOk → null
func (Option[T]) MustGet ¶ added in v0.30.0
func (b Option[T]) MustGet() T
MustGet returns the option's value or panics if the option is not ok.
func (Option[T]) Or ¶ added in v0.30.0
func (b Option[T]) Or(t T) T
Or returns the option's value provided that the option is ok, otherwise t.
func (Option[T]) OrCall ¶ added in v0.30.0
func (b Option[T]) OrCall(fn func() T) (_ T)
OrCall returns the option's value provided that it is ok, otherwise the result of calling fn.
func (Option[T]) OrEmpty ¶ added in v0.30.0
func (b Option[T]) OrEmpty() (_ T)
OrEmpty returns the option's value provided that it is ok, otherwise the zero value for T. It is a more readable alias for OrZero when T is string.
func (Option[T]) OrFalse ¶ added in v0.30.0
OrFalse returns the option's value provided that it is ok, otherwise false. It is a readable alias for OrZero when the type is bool.
func (Option[T]) OrZero ¶ added in v0.30.0
func (b Option[T]) OrZero() (_ T)
OrZero returns the option's value provided that it is ok, otherwise the zero value for T. See OrEmpty and OrFalse for more readable aliases of OrZero when T is string or bool.
func (Option[T]) RemoveIf ¶ added in v0.30.0
RemoveIf returns a not-ok option provided that applying fn to an ok option's value returns true, or the original option otherwise. It is the filter operation with negation. Since Go doesn't offer a convenient lambda syntax for constructing the negation of a function's output, having negation built-in is both a convenience and keeps consuming code readable.
func (Option[T]) ToAny ¶ added in v0.30.0
ToAny returns an option of the result of applying fn to the option's value provided that the option is ok, or not-ok otherwise.
func (Option[T]) ToBool ¶ added in v0.30.0
ToBool returns an option of the result of applying fn to the option's value provided that the option is ok, or not-ok otherwise.
func (Option[T]) ToByte ¶ added in v0.30.0
ToByte returns an option of the result of applying fn to the option's value provided that the option is ok, or not-ok otherwise.
func (Option[T]) ToError ¶ added in v0.30.0
ToError returns an option of the result of applying fn to the option's value provided that the option is ok, or not-ok otherwise.
func (Option[T]) ToInt ¶ added in v0.30.0
ToInt returns an option of the result of applying fn to the option's value provided that the option is ok, or not-ok otherwise.
func (Option[T]) ToOpt ¶ added in v0.30.0
func (b Option[T]) ToOpt() (_ *T)
ToOpt returns a pointer-based pseudo-option of the pointed-at value provided that the option is ok, or not-ok otherwise. By convention, in consuming code, we suffix a pseudo-option's variable name with an "Opt" suffix to clarify the pointer's meaning and use, hence "ToOpt".
func (Option[T]) ToRune ¶ added in v0.30.0
ToRune returns an option of the result of applying fn to the option's value provided that the option is ok, or not-ok otherwise.
func (Option[T]) ToString ¶ added in v0.30.0
ToString returns an option of the result of applying fn to the option's value provided that the option is ok, or not-ok otherwise.
func (*Option[T]) UnmarshalJSON ¶ added in v0.30.0
UnmarshalJSON deserializes Option: null → NotOk, value → Ok(value)