Documentation ¶
Overview ¶
Package optional provides an “option type,” which can either be empty or hold a value. In that respect, it's very similar to an ordinary pointer type, except it has methods that make its possible emptiness more explicit.
Index ¶
- Variables
- type Value
- func (o Value[T]) Get() (result T, err error)
- func (o Value[T]) GoString() string
- func (o Value[T]) If(fn func(T))
- func (o Value[T]) MarshalJSON() ([]byte, error)
- func (o Value[T]) MustGet() T
- func (o Value[T]) OrElse(v T) T
- func (o Value[T]) OrElseGet(calculateFallback func() T) T
- func (o Value[_]) Present() bool
- func (o Value[T]) String() string
- func (o *Value[T]) UnmarshalJSON(data []byte) error
Constants ¶
This section is empty.
Variables ¶
var ErrEmpty = errors.New("value not present")
ErrEmpty indicates that an optional value was empty when its value was requested.
Functions ¶
This section is empty.
Types ¶
type Value ¶
type Value[T any] []T
Value is a type that may or may not hold a value. Its interface is modeled on Java's java.util.Optional type and C++'s std::optional.
Value implements the following interfaces:
func Transform ¶
Transform applies the given function to the optional value if the input value is non-empty, and returns a new optional of the corresponding return type holding the returned value. Returns an empty value if the input is empty.
func TransformWithError ¶
TransformWithError applies the given function to the optional value if the input value is non-empty, and returns a new optional of the corresponding return type holding the returned value. Returns an empty value if the input is empty. If the transform function returns an error, then an empty value and that error are returned.
func (Value[T]) Get ¶
Get returns the current value, if there is one. If the Value is empty, then ErrEmpty is returned, and the value result is unspecified.
func (Value[T]) GoString ¶
GoString fornats the Value as Go code, providing an implementation for the %#v format string.
func (Value[T]) If ¶
func (o Value[T]) If(fn func(T))
If calls the given function if the Value holds a value. If Value is empty, then If is a no-op.
func (Value[T]) MarshalJSON ¶
MarshalJSON converts the value to a JSON value. If the Value is empty, then the JSON result is null.
func (Value[T]) MustGet ¶
func (o Value[T]) MustGet() T
MustGet returns the current value, if there is one. If the Value is empty, then MustGet panics with ErrEmpty.
func (Value[T]) OrElse ¶
func (o Value[T]) OrElse(v T) T
OrElse returns the stored value, if there is one. If the Value is empty, then OrElse returns the given argument.
func (Value[T]) OrElseGet ¶
func (o Value[T]) OrElseGet(calculateFallback func() T) T
OrElseGet returns the stored value, if there is one. If the Value is empty, then OrElseGet calls the given function and returns the result. Use this instead of OrElse when calculation of the fallback value is relatively expensive. It will only be calculated when needed.
func (Value[_]) Present ¶
Present returns true if there is a value stored, false if the Value is empty.
func (Value[T]) String ¶
String returns the string representation of the stored value, if present. Otherwise, it returns None.
func (*Value[T]) UnmarshalJSON ¶
UnmarshalJSON converts the given JSON value to an optional Value[T]. If the JSON value is null, then the result is empty. Otherwise, the JSON is unmarshaled in the same way values of type T are unmarshaled.