result

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2025 License: MIT Imports: 3 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrEmptyResult = fmt.Errorf("result is error but error was nil")

Functions

func If added in v0.3.0

func If[Out, T any](r Result[T], okFn func(T) Out, errFn func(error) Out) Out

Types

type Result

type Result[T any] struct {
	// contains filtered or unexported fields
}

Result[T] is a generic pseudo-enum, used for returning results with errors. The result type eliminates the need to return nil pointers, sentinal type zero values, or partial results when an error has occured. Instead, the result type can be in one of two states: Ok(T) or Err(error).

func AndThen

func AndThen[T, U any](r Result[T], fn func(T) Result[U]) Result[U]

func Err

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

Create an error Result[T]

func FlatMap added in v0.3.0

func FlatMap[T, U any](r Result[T], fn func(T) Result[U]) Result[U]

func Map

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

func Map2

func Map2[T, U, V any](r Result[T], s Result[U], fn func(T, U) V) Result[V]

func Map3

func Map3[T, U, V, W any](r Result[T], s Result[U], t Result[V], fn func(T, U, V) W) Result[W]

func Ok

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

Create a successful Result[T]

func (Result[T]) Err

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

If IsErr returns true, Err is guaranteed to return an error value. If Result[T] was initialised as a zero value, or if it was initilised as Err(nil), then this function will return an ErrEmptyResult.

func (Result[T]) Expect

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

func (Result[T]) ExpectErr

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

func (Result[T]) IsErr

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

A Result[T] is considered to be in an error state if there is no value. IsErr will always be opposite to IsOk.

func (Result[T]) IsOk

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

Is the result in the Ok state

func (Result[T]) MapError

func (r Result[T]) MapError(fn func(e error) error) Result[T]

func (Result[T]) Ok

func (r Result[T]) Ok(out *T) error

func (Result[T]) Unwrap

func (r Result[T]) Unwrap() T

func (Result[T]) UnwrapOr

func (r Result[T]) UnwrapOr(defaultValue T) T

func (Result[T]) UnwrapOrElse

func (r Result[T]) UnwrapOrElse(fn func(error) T) T

func (Result[T]) Value

func (r Result[T]) Value() option.Option[T]

Jump to

Keyboard shortcuts

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