Documentation
¶
Overview ¶
Error handling with the Result type.
Result[T] is the type used for returning and propagating errors. It is an enum with the variants, Ok(T), representing success and containing a value, and Err(E), representing error and containing an error value.
Index ¶
- func Collect[T any](seq iter.Seq[Result[T]]) (values []T, err error)
- func Collect2[T any](seq iter.Seq2[T, error]) (values []T, err error)
- func MapOr[T any, U any](r Result[T], def U, f func(T) U) U
- func MapOrElse[T any, U any](r Result[T], def func() U, f func(T) U) U
- type Result
- func And[T, U any](res1 Result[T], res2 Result[U]) Result[U]
- func AndThen[T, U any](res Result[T], op func(T) Result[U]) Result[U]
- func Err[T any](err error) Result[T]
- func Flatten[T any](r Result[Result[T]]) Result[T]
- func Map[T any, U any](r Result[T], f func(T) U) Result[U]
- func Ok[T any](value T) Result[T]
- func Wrap[T any](value T, err error) Result[T]
- func (r Result[T]) And(res Result[T]) Result[T]
- func (r Result[T]) AndThen(op func(T) Result[T]) Result[T]
- func (r Result[T]) Expect(msg string) T
- func (r Result[T]) ExpectErr(msg string) error
- func (r Result[T]) Inspect(f func(T)) Result[T]
- func (r Result[T]) InspectErr(f func(error)) Result[T]
- func (r Result[T]) IsErr() bool
- func (r Result[T]) IsErrAnd(f func(error) bool) bool
- func (r Result[T]) IsOk() bool
- func (r Result[T]) IsOkAnd(f func(T) bool) bool
- func (o Result[T]) Iter() iter.Seq[T]
- func (r Result[T]) Map(f func(T) T) Result[T]
- func (r Result[T]) MapErr(f func(error) error) Result[T]
- func (r Result[T]) MapOr(def T, f func(T) T) T
- func (r Result[T]) MapOrElse(def func() T, f func(T) T) T
- func (r Result[T]) Or(res Result[T]) Result[T]
- func (r Result[T]) OrElse(op func(error) Result[T]) Result[T]
- func (r Result[T]) String() string
- func (r Result[T]) Unwrap() T
- func (r Result[T]) UnwrapErr() error
- func (r Result[T]) UnwrapOr(def T) T
- func (r Result[T]) UnwrapOrDefault() (v T)
- func (r Result[T]) UnwrapOrElse(f func() T) T
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Collect ¶
Collect iterates over a sequence of Result[T] values, collecting all successful values into a slice.
If any Result in the sequence is an error, Collect returns nil and the encountered error immediately. Otherwise, it returns the slice of unwrapped values and a nil error.
Example (Err) ¶
// Some I/O results
s := slices.Values([]Result[string]{
Ok("hello"),
Err[string](io.EOF),
Ok("world"),
})
fmt.Println(Collect(s))
Output: [] EOF
Example (Ok) ¶
// Some I/O results
s := slices.Values([]Result[string]{
Ok("hello"),
Ok("world"),
})
fmt.Println(Collect(s))
Output: [hello world] <nil>
func Collect2 ¶
Collect2 iterates over a sequence of values and errors provided by seq.
It collects all values into a slice until an error is encountered. If an error occurs during iteration, it returns nil and the error. Otherwise, it returns the collected values and a nil error.
Example ¶
// Some I/O results
s := slices.All([]error{nil, io.EOF, nil})
res, err := Collect2(s)
fmt.Println(err, res)
Output: EOF []
func MapOr ¶
Returns the provided default (if Err), or applies a function to the contained value (if Ok).
Example ¶
sizeof := func(s string) int { return len(s) }
ok := Ok("hello world!")
fmt.Println(MapOr(ok, -1, sizeof))
err := Err[string](io.EOF)
fmt.Println(MapOr(err, -1, sizeof))
Output: 12 -1
func MapOrElse ¶
Maps a Result[T] to U by applying fallback function default to a contained Err value, or function f to a contained Ok value.
Example ¶
sizeof := func(s string) int { return len(s) }
ok := Ok("hello world!")
fmt.Println(MapOrElse(ok, func() int { return 0 }, sizeof))
err := Err[string](io.EOF)
fmt.Println(MapOrElse(err, func() int { return 0 }, sizeof))
Output: 12 0
Types ¶
type Result ¶
type Result[T any] struct { // contains filtered or unexported fields }
Result is a type that represents either success (Ok) or failure (Err).
func Flatten ¶
Converts from Result[Result[T]] to Result[T].
Example ¶
x := Ok(Ok(6)) fmt.Println(Flatten(x)) y := Ok(Err[int](io.EOF)) fmt.Println(Flatten(y))
Output: Ok(6) Err(EOF)
func Map ¶
Maps a Result[T] to Result[U] by applying a function to a contained Ok value, leaving an Err value untouched.
Example ¶
sizeof := func(s string) int { return len(s) }
ok := Ok("hello world!")
fmt.Println(Map(ok, sizeof))
err := Err[string](io.EOF)
fmt.Println(Map(err, sizeof))
Output: Ok(12) Err(EOF)
func (Result[T]) Expect ¶
Returns the contained Ok value, or panics if the value is an Err, with a panic message including the passed message, and the content of the Err..
func (Result[T]) ExpectErr ¶
Returns the contained Err value, or panics if the value is an Ok, with a panic message including the passed message, and the content of the Ok.
func (Result[T]) InspectErr ¶
Calls a function with a reference to the contained value if Err.
func (Result[T]) IsErrAnd ¶
Returns true if the result is Err and the value inside of it matches a predicate.
func (Result[T]) IsOkAnd ¶
Returns true if the result is Ok and the value inside of it matches a predicate.
func (Result[T]) Iter ¶
Returns an iterator over the possibly contained value.
The iterator yields one value if the result is Ok, otherwise none.
func (Result[T]) Map ¶
Maps a Result[T] by applying a function to a contained Ok value, leaving an Err value untouched.
func (Result[T]) MapErr ¶
Maps a Result[T] by applying a function to a contained Err value, leaving an Ok value untouched.
func (Result[T]) MapOr ¶
func (r Result[T]) MapOr(def T, f func(T) T) T
Returns the provided default (if Err), or applies a function to the contained value (if Ok).
func (Result[T]) MapOrElse ¶
func (r Result[T]) MapOrElse(def func() T, f func(T) T) T
Maps a Result[T] by applying fallback function default to a contained Err value, or function f to a contained Ok value.
func (Result[T]) Unwrap ¶
func (r Result[T]) Unwrap() T
Returns the contained Ok value, or panics if the value is an Err.
func (Result[T]) UnwrapOr ¶
func (r Result[T]) UnwrapOr(def T) T
Returns the contained Ok value or a provided default value.
func (Result[T]) UnwrapOrDefault ¶
func (r Result[T]) UnwrapOrDefault() (v T)
Returns the contained Ok value or a default.
func (Result[T]) UnwrapOrElse ¶
func (r Result[T]) UnwrapOrElse(f func() T) T
Returns the contained Ok value or computes it from a closure.