result

package
v1.1.29 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package result - Because nil checks are so last century

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option added in v1.1.27

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

Option represents a value that may or may not exist

func None added in v1.1.27

func None[T any]() Option[T]

None creates an empty Option

func Some added in v1.1.27

func Some[T any](value T) Option[T]

Some creates an Option containing a value

func Transpose added in v1.1.27

func Transpose[T any](r Result[Option[T]]) Option[Result[T]]

Transpose converts Result[Option[T]] to Option[Result[T]]

func (Option[T]) IsNone added in v1.1.27

func (o Option[T]) IsNone() bool

IsNone returns true if the Option contains no value

func (Option[T]) IsSome added in v1.1.27

func (o Option[T]) IsSome() bool

IsSome returns true if the Option contains a value

func (Option[T]) UnwrapOr added in v1.1.27

func (o Option[T]) UnwrapOr(defaultValue T) T

UnwrapOr returns the contained value or a default

func (Option[T]) UnwrapOrPanic added in v1.1.27

func (o Option[T]) UnwrapOrPanic() T

UnwrapOrPanic returns the contained value or panics

type Pair added in v1.1.27

type Pair[T, U any] struct {
	First  T
	Second U
}

Pair holds two values of potentially different types

type Result

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

Result represents a value that might exist Or might be busy generating stack traces

func Collect added in v1.1.27

func Collect[T any](results []Result[T]) Result[[]T]

Collect turns a slice of Results into a Result of slice

func Err

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

Err creates a Result for when reality meets expectations

func FlatMap

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

FlatMap is like Map but for when you want to fail twice

func FromOption added in v1.1.27

func FromOption[T any](opt Option[T], err error) Result[T]

FromOption converts an Option to a Result with custom error

func Map

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

Map transforms success into different success Or keeps the failure, because consistency

func NewResult

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

NewResult creates a new coin flip of success/failure

func Ok

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

Ok creates a Result for when things actually work (Don't get used to it)

func Try added in v1.1.27

func Try[T any](fn func() T) Result[T]

Try converts a panic into a Result

func Zip added in v1.1.27

func Zip[T, U any](r1 Result[T], r2 Result[U]) Result[Pair[T, U]]

Zip combines two Results into one

func (Result[T]) AndThen

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

AndThen chains operations because one point of failure isn't enough

func (Result[T]) Err added in v1.1.27

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

Err returns the error if present, nil otherwise

func (Result[T]) Expect

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

Expect returns the value or panics with style

func (Result[T]) ExpectErr

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

ExpectErr returns the error or panics because you expected failure and got success (how dare you succeed?)

func (Result[T]) Filter added in v1.1.27

func (r Result[T]) Filter(predicate func(T) bool) Result[T]

Filter turns success into failure if a condition isn't met

func (Result[T]) Inspect added in v1.1.27

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

Inspect lets you peek at success without changing it

func (Result[T]) InspectErr added in v1.1.27

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

InspectErr lets you peek at failure without changing it

func (Result[T]) IsErr

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

IsErr checks if Murphy's Law is still in effect

func (Result[T]) IsOk

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

IsOk checks if your optimism was justified

func (Result[T]) Match added in v1.1.27

func (r Result[T]) Match(ok func(T), err func(error))

Match is for when you want to handle both cases elegantly

func (Result[T]) OrElse

func (r Result[T]) OrElse(fn func() Result[T]) Result[T]

OrElse provides a backup plan when things go wrong

func (Result[T]) Unwrap

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

Unwrap opens Schrödinger's box

func (Result[T]) UnwrapErr

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

UnwrapErr returns the error or panics because success wasn't in the plan

func (Result[T]) UnwrapOr

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

UnwrapOr returns the value or your backup plan

func (Result[T]) UnwrapOrDefault

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

UnwrapOrDefault returns whatever value your type holds.

func (Result[T]) UnwrapOrElse

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

UnwrapOrElse returns the value or makes you work for a default

func (Result[T]) UnwrapOrPanic

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

UnwrapOrPanic returns the value or throws in the towel

Jump to

Keyboard shortcuts

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