Documentation
¶
Overview ¶
Package result provides a generic Result type for Go inspired by Rust's Result<T, E>.
Index ¶
- func Match[T any, U any](r Result[T], onOk func(T) U, onErr func(error) U) U
- type Result
- func All[T any](results []Result[T]) Result[[]T]
- func Err[T any](err error) Result[T]
- func Errf[T any](format string, args ...any) Result[T]
- func FlatMap[T any, U any](r Result[T], fn func(T) Result[U]) Result[U]
- func Map[T any, U any](r Result[T], fn func(T) U) Result[U]
- func Ok[T any](value T) Result[T]
- func Try[T any](fn func() (T, error)) Result[T]
- func (r Result[T]) Error() error
- func (r Result[T]) Expect(msg string) T
- func (r Result[T]) Filter(predicate func(T) bool, errFn func(T) error) Result[T]
- func (r Result[T]) IsErr() bool
- func (r Result[T]) IsErrAnd(predicate func(error) bool) bool
- func (r Result[T]) IsOk() bool
- func (r Result[T]) IsOkAnd(predicate func(T) bool) bool
- func (r Result[T]) Or(other Result[T]) Result[T]
- func (r Result[T]) OrElse(fn func(error) Result[T]) Result[T]
- func (r Result[T]) String() string
- func (r Result[T]) Tap(fn func(T)) Result[T]
- func (r Result[T]) TapErr(fn func(error)) Result[T]
- func (r Result[T]) Unwrap() T
- func (r Result[T]) UnwrapOr(def T) T
- func (r Result[T]) UnwrapOrElse(fn func(error) T) T
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Result ¶
type Result[T any] struct { // contains filtered or unexported fields }
Result represents either a success value (Ok) or an error value (Err).
func All ¶
All collects a slice of Results into a Result containing a slice of values. Returns the first error encountered.
func (Result[T]) Expect ¶ added in v0.3.0
Expect returns the success value or panics with the given message if the Result is an error.
func (Result[T]) Filter ¶ added in v0.4.0
Filter returns Err if the Ok value doesn't match the predicate. errFn produces the error from the value.
func (Result[T]) IsErrAnd ¶ added in v0.4.0
IsErrAnd returns true if the Result is Err and the error matches the predicate.
func (Result[T]) IsOkAnd ¶ added in v0.4.0
IsOkAnd returns true if the Result is Ok and the value matches the predicate.
func (Result[T]) Or ¶ added in v0.3.0
Or returns the Result if it is Ok, otherwise returns the provided fallback Result.
func (Result[T]) OrElse ¶ added in v0.4.0
OrElse returns r if Ok, otherwise calls fn with the error to produce an alternative Result.
func (Result[T]) String ¶ added in v0.3.0
String returns a human-readable representation of the Result.
func (Result[T]) Tap ¶ added in v0.4.0
Tap calls fn with the Ok value for side effects, then returns the original Result unchanged.
func (Result[T]) TapErr ¶ added in v0.4.0
TapErr calls fn with the error for side effects, then returns the original Result unchanged.
func (Result[T]) Unwrap ¶
func (r Result[T]) Unwrap() T
Unwrap returns the success value or panics if the Result is an error.
func (Result[T]) UnwrapOr ¶
func (r Result[T]) UnwrapOr(def T) T
UnwrapOr returns the success value or the provided default.
func (Result[T]) UnwrapOrElse ¶
UnwrapOrElse returns the success value or calls the provided function.