result

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Match

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

Match applies one of two functions based on the result state. The Ok function is called if result is successful, the Err function if it's an error. This enables functional-style branching on the result.

func Partition

func Partition[T any](results []Result[T]) (ok []T, errs []error)

Partition separates a slice of Results into two slices: successful values and errors.

Types

type Result

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

Result is a type-safe way to return values or errors.

func AndThen

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

AndThen chains operations that return Result, flattening the result. If the result is an error, it returns an error result without calling the function. This enables chaining operations that can fail (like FlatMap in functional programming).

func Err

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

Err creates an error result.

func Fold

func Fold[T, U any](results []Result[T], initial U, fn func(acc U, val T) U) Result[U]

Fold reduces a slice of Results into a single Result by applying fn cumulatively. The accumulator starts with initial value. If any result is an error, Fold returns that error immediately (short-circuit).

func FoldAll

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

FoldAll reduces a slice of Results into a single Result by collecting all Ok values. If any result is an error, FoldAll returns that error immediately (short-circuit). Returns a slice of all values.

func Map

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

Map applies function to value if successful, passes through error.

func MockSuccess

func MockSuccess[T any](value T, message string) Result[T]

MockSuccess creates a successful result with warning message.

func Ok

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

Ok creates a successful result.

func PartitionResults

func PartitionResults[T any](results []Result[T]) (ok, errs []Result[T])

PartitionResults separates results into successful and failed Results.

func Sequence

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

Sequence converts a slice of Results to a Result of slice. If all results are Ok, returns Ok with all values. If any result is an error, returns Err with the first error encountered.

func Traverse

func Traverse[T, U any](items []T, fn func(T) Result[U]) Result[[]U]

Traverse applies fn to each item in items and sequences the results. If all results are Ok, returns Ok with all transformed values. If any result is an error, returns Err with the first error encountered.

func (Result[T]) Error

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

Error returns error (panics on success).

func (Result[T]) Filter

func (r Result[T]) Filter(predicate func(T) bool, errMsg string) Result[T]

Filter returns the result if predicate is true, otherwise returns an error. If the result already has an error, it passes through the error.

func (Result[T]) FilterWithError

func (r Result[T]) FilterWithError(predicate func(T) bool, err error) Result[T]

FilterWithError returns the result if predicate is true, otherwise returns the provided error. If the result already has an error, it passes through the error.

func (Result[T]) IsErr

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

IsErr returns true if result has error.

func (Result[T]) IsOk

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

IsOk returns true if result is successful.

func (Result[T]) OrElse

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

OrElse returns the current result if successful, otherwise returns the fallback result. Useful for providing fallback values when operations fail.

func (Result[T]) SafeError

func (r Result[T]) SafeError() (error, bool)

SafeError returns error and ok boolean (never panics).

func (Result[T]) SafeValue

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

SafeValue returns value and error (never panics).

func (Result[T]) Tap

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

Tap applies a side-effect function to the value if successful. Returns the original result unchanged, useful for logging or other side effects.

func (Result[T]) TapErr

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

TapErr applies a side-effect function to the error if present. Returns the original result unchanged, useful for error logging.

func (Result[T]) Unless

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

Unless executes the given function if the result is an error, otherwise does nothing. Returns the original result for chaining.

func (Result[T]) Unwrap

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

Unwrap returns value and error.

func (Result[T]) UnwrapOr

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

UnwrapOr returns value or default if error.

func (Result[T]) Validate

func (r Result[T]) Validate(predicate func(T) bool, errorMsg string) Result[T]

Validate checks if the value satisfies the predicate. If the predicate returns false, it returns an error with the given message. If the result already has an error, it passes through the error.

func (Result[T]) ValidateWithError

func (r Result[T]) ValidateWithError(predicate func(T) bool, err error) Result[T]

ValidateWithError checks if the value satisfies the predicate. If the predicate returns false, it returns the provided error. If the result already has an error, it passes through the error.

func (Result[T]) Value

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

Value returns value (panics if error).

func (Result[T]) When

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

When executes the given function if the result is Ok, otherwise does nothing. Returns the original result for chaining.

Jump to

Keyboard shortcuts

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