res

package
v0.0.0-...-e1eb702 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 9, 2025 License: MIT Imports: 2 Imported by: 0

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Collect

func Collect[T any](seq iter.Seq[Result[T]]) (values []T, err error)

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

func Collect2[T any](seq iter.Seq2[T, error]) (values []T, err error)

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

func MapOr[T any, U any](r Result[T], def U, f func(T) U) U

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

func MapOrElse[T any, U any](r Result[T], def func() U, f func(T) U) U

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 And

func And[T, U any](res1 Result[T], res2 Result[U]) Result[U]

Returns res2 if the res1 is Ok, otherwise returns the Err value of res1.

func AndThen

func AndThen[T, U any](res Result[T], op func(T) Result[U]) Result[U]

Calls op if the res is Ok, otherwise returns the Err value of self.

func Err

func Err[T any](err error) Result[T]

Contains the error value.

func Flatten

func Flatten[T any](r Result[Result[T]]) Result[T]

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

func Map[T any, U any](r Result[T], f func(T) U) Result[U]

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 Ok

func Ok[T any](value T) Result[T]

Contains the success value.

func Wrap

func Wrap[T any](value T, err error) Result[T]

Wrap a value and error

func (Result[T]) And

func (r Result[T]) And(res Result[T]) Result[T]

Returns res if the r is Ok, otherwise returns the Err value of r.

func (Result[T]) AndThen

func (r Result[T]) AndThen(op func(T) Result[T]) Result[T]

Calls op if the r is Ok, otherwise returns the Err value of self.

func (Result[T]) Expect

func (r Result[T]) Expect(msg string) T

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

func (r Result[T]) ExpectErr(msg string) error

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]) Inspect

func (r Result[T]) Inspect(f func(T)) Result[T]

Calls a function with a reference to the contained value if Ok.

func (Result[T]) InspectErr

func (r Result[T]) InspectErr(f func(error)) Result[T]

Calls a function with a reference to the contained value if Err.

func (Result[T]) IsErr

func (r Result[T]) IsErr() bool

Returns true if the result is Err.

func (Result[T]) IsErrAnd

func (r Result[T]) IsErrAnd(f func(error) bool) bool

Returns true if the result is Err and the value inside of it matches a predicate.

func (Result[T]) IsOk

func (r Result[T]) IsOk() bool

Returns true if the result is Ok.

func (Result[T]) IsOkAnd

func (r Result[T]) IsOkAnd(f func(T) bool) bool

Returns true if the result is Ok and the value inside of it matches a predicate.

func (Result[T]) Iter

func (o Result[T]) Iter() iter.Seq[T]

Returns an iterator over the possibly contained value.

The iterator yields one value if the result is Ok, otherwise none.

func (Result[T]) Map

func (r Result[T]) Map(f func(T) T) Result[T]

Maps a Result[T] by applying a function to a contained Ok value, leaving an Err value untouched.

func (Result[T]) MapErr

func (r Result[T]) MapErr(f func(error) error) Result[T]

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]) Or

func (r Result[T]) Or(res Result[T]) Result[T]

Returns res if the res is Err, otherwise returns the Ok value of self.

func (Result[T]) OrElse

func (r Result[T]) OrElse(op func(error) Result[T]) Result[T]

Calls op if the result is Err, otherwise returns the Ok value of self.

func (Result[T]) String

func (r Result[T]) String() string

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]) UnwrapErr

func (r Result[T]) UnwrapErr() error

Returns the contained Err value, or panics if the value is an Ok.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL