result

package
v1.2.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Must

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

Must panics if the result is an error, otherwise returns the value.

Types

type Result

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

Result represents either a success value or an error.

Example:

r := result.Ok(42)
if r.IsOk() { ... }

func Err

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

Err creates an error Result.

func FlatMap

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

FlatMap transforms the value with a function that returns a Result.

func From

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

From wraps a traditional (value, error) tuple into a Result.

func Map

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

Map transforms the value if successful.

Example:

r := result.Ok(10)
r2 := result.Map(r, func(i int) string { return strconv.Itoa(i) })

func Ok

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

Ok creates a successful Result.

func Try

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

Try executes fn and wraps the result.

func (Result[T]) Error

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

Error returns the error, or nil if successful.

func (Result[T]) IsErr

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

IsErr returns true if the result contains an error.

func (Result[T]) IsOk

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

IsOk returns true if the result is successful.

func (Result[T]) Unwrap

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

Unwrap returns the value or panics if error.

Notes:

Panics if the result is an error.

func (Result[T]) UnwrapOr

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

UnwrapOr returns the value or the provided default.

func (Result[T]) UnwrapOrElse

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

UnwrapOrElse returns the value or calls fn to get a default.

func (Result[T]) Value

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

Value returns the value and error (traditional Go style).

Jump to

Keyboard shortcuts

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