Documentation
¶
Overview ¶
Package rslt provides a Result type for operations that may fail.
Index ¶
- func CollectAll[R any](results []Result[R]) ([]R, error)
- func CollectErr[R any](results []Result[R]) []error
- func CollectOk[R any](results []Result[R]) []R
- func CollectOkAndErr[R any](results []Result[R]) ([]R, []error)
- func Fold[R, T any](res Result[R], onErr func(error) T, onOk func(R) T) T
- func Lift[A, R any](fn func(A) (R, error)) func(A) Result[R]
- type PanicError
- type Result
- func (r Result[R]) Convert(fn func(R) R) Result[R]
- func (r Result[R]) FlatMap(fn func(R) Result[R]) Result[R]
- func (r Result[R]) Get() (_ R, _ bool)
- func (r Result[R]) GetErr() (_ error, _ bool)
- func (r Result[R]) IfErr(fn func(error))
- func (r Result[R]) IfOk(fn func(R))
- func (r Result[R]) IsErr() bool
- func (r Result[R]) IsOk() bool
- func (r Result[R]) MapErr(fn func(error) error) Result[R]
- func (r Result[R]) MustGet() R
- func (r Result[R]) Or(defaultVal R) R
- func (r Result[R]) OrCall(fn func() R) R
- func (r Result[R]) Unpack() (R, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CollectAll ¶
CollectAll returns all values if every result is Ok, or the first error by index order otherwise. Note: for concurrent results from [FanOut], index order may differ from completion order.
func CollectErr ¶
CollectErr returns the errors from all Err results, preserving order.
func CollectOkAndErr ¶
CollectOkAndErr splits results into Ok values and Err errors in a single pass, preserving order.
Types ¶
type PanicError ¶
PanicError wraps a recovered panic value and its stack trace. It is stored as *PanicError in Err results. Callers detect it via errors.As(err, &pe) where pe is *PanicError.
func (*PanicError) Error ¶
func (e *PanicError) Error() string
Error returns a string representation of the panic value.
func (*PanicError) Unwrap ¶
func (e *PanicError) Unwrap() error
Unwrap returns the panic value if it is an error, or nil otherwise. This preserves error chains: errors.Is(resultErr, context.Canceled) works when fn panics with a wrapped context error.
type Result ¶
type Result[R any] struct { // contains filtered or unexported fields }
Result represents the outcome of an operation that may fail. It holds either a value (Ok) or an error (Err).
The zero value is Ok containing R's zero value. This means an uninitialized Result (e.g. a struct field never assigned) silently reports success. Always construct results explicitly via Ok, Err, or Of.
func FlatMap ¶
FlatMap returns the result of applying fn to the value if Ok, or Err with same error if Err.
func Map ¶
Map returns the result of applying fn to the value if res is Ok, or an Err with the same error if res is Err.
func Of ¶
Of returns a Result from a (value, error) pair — the signature returned by most Go functions. If err is non-nil the result is Err; otherwise Ok.
func (Result[R]) Convert ¶
Convert returns the result of applying fn to the value if r is Ok, or r unchanged if r is Err. For cross-type mapping (R -> S), use the standalone Map function. Convert is the same-type method form; Go does not allow generic methods with extra type parameters, so cross-type mapping requires a standalone function.
func (Result[R]) FlatMap ¶
FlatMap returns the result of applying fn to the value if Ok, or r unchanged if Err.
func (Result[R]) Get ¶
Get returns the value and true if r is Ok, or the zero value and false if r is Err.
func (Result[R]) GetErr ¶
GetErr returns the error and true if r is Err, or nil and false if r is Ok.
func (Result[R]) IfOk ¶
func (r Result[R]) IfOk(fn func(R))
IfOk calls fn with the value if r is Ok.
func (Result[R]) MapErr ¶
MapErr returns a Result with the error transformed by fn if r is Err, or r unchanged if r is Ok. Useful for wrapping or annotating errors without losing the Result context. Panics if fn returns nil (same as Err).
func (Result[R]) MustGet ¶
func (r Result[R]) MustGet() R
MustGet returns the value if r is Ok, or panics with the error if r is Err. The panic value wraps the original error, preserving error chains for errors.Is and errors.As after recovery.
func (Result[R]) Or ¶
func (r Result[R]) Or(defaultVal R) R
Or returns the value if r is Ok, or defaultVal if r is Err.