result

package
v1.20.0 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2025 License: MIT Imports: 1 Imported by: 27

Documentation

Overview

Package result provides helper functions for working with Result types.

Result represents a value that can be either Ok(T) or Err(error). It provides a Rust-inspired way to handle errors in Go without using the traditional (T, error) tuple pattern, enabling chainable error handling.

Examples

// Create a Result
success := result.Ok(42)
failure := result.TryErr[int](errors.New("error"))

// Chain operations
value := success.
	Map(func(x int) int { return x * 2 }).
	UnwrapOr(0) // Output: 84

// Handle errors
err := failure.
	MapErr(func(e error) error { return fmt.Errorf("wrapped: %w", e) }).
	Err() // Returns the error

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains

func Contains[T comparable](r Result[T], x T) bool

Contains returns true if the result is an Ok value containing the given value.

func Contains2

func Contains2[T comparable](v T, err error, x T) bool

Contains2 returns true if the result is an Ok value containing the given value.

func MapOr

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

MapOr returns the provided default (if error), or applies a function to the contained value (if no error), Arguments passed to map_or are eagerly evaluated; if you are passing the result of a function call, it is recommended to use MapOrElse, which is lazily evaluated.

func MapOr2

func MapOr2[T any, U any](v T, err error, defaultOk U, f func(T) U) U

MapOr2 maps a value and an error to U by applying a function to the value, or returns the default if error.

Examples

var v = 1
var err = nil
var result = MapOr2(v, err, func(v int) int { return v * 2 })
assert.Equal(t, core.Ok[int](2), result)

func MapOrElse

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

MapOrElse maps a Result[T] to U by applying fallback function default to a contained error, or function f to a contained Ok value. This function can be used to unpack a successful result while handling an error.

func MapOrElse2

func MapOrElse2[T any, U any](v T, err error, defaultFn func(error) U, f func(T) U) U

MapOrElse2 maps a value and an error to U by applying a function to the value, or applies the default function if error.

Examples

var v = 1
var err = nil
var result = MapOrElse2(v, err, func(err error) int { return 0 }, func(v int) int { return v * 2 })
assert.Equal(t, core.Ok[int](2), result)

func ToError

func ToError(r VoidResult) error

ToError converts VoidResult to a standard Go error. Returns nil if IsOk() is true, otherwise returns the error.

Example:

```go
var result result.VoidResult = result.RetVoid(err)
if err := result.ToError(result); err != nil {
	return err
}
```

func UnwrapErrOr

func UnwrapErrOr(r VoidResult, defaultErr error) error

UnwrapErrOr returns the contained error value or a provided default for VoidResult.

Example:

```go
var result result.VoidResult = result.RetVoid(err)
err := result.UnwrapErrOr(result, errors.New("default error"))
```

Types

type Result

type Result[T any] = core.Result[T]

Result is an alias for core.Result[T]. This allows using result.Result[T] instead of core.Result[T].

Result represents a value that can be either Ok(T) or Err(error). It provides a Rust-inspired way to handle errors in Go without using the traditional (T, error) tuple pattern.

func And

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

And returns `r1` if `r1` is Err, otherwise returns `r2`.

func And2

func And2[T any, U any](v1 T, err1 error, v2 U, err2 error) Result[U]

And2 returns `Ret(v1, err1)` if `r1` is `Err`, otherwise returns `Ret(v2, err2)`.

Examples

var v1 = 1
var err1 = nil
var v2 = 2
var err2 = nil
var result = And2(v1, err1, v2, err2)
assert.Equal(t, core.Ok[int](2), result)

var v1 = 1
var err1 = errors.New("error1")
var v2 = 2
var err2 = nil
var result = And2(v1, err1, v2, err2)
assert.Equal(t, "error1", result.Err().Error())

var v1 = 1
var err1 = nil
var v2 = 2
var err2 = errors.New("error2")
var result = And2(v1, err1, v2, err2)
assert.Equal(t, "error2", result.Err().Error())

var v1 = 1
var err1 = errors.New("error1")
var v2 = 2
var err2 = errors.New("error2")
var result = And2(v1, err1, v2, err2)
assert.Equal(t, "error1", result.Err().Error())

func AndThen

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

AndThen calls op if the result is Ok, otherwise returns the error of self. This function can be used for control flow based on Result values.

func AndThen2

func AndThen2[T any, U any](r Result[T], op func(T) (U, error)) Result[U]

AndThen2 calls op if the result is Ok, otherwise returns the error of self. This function can be used for control flow based on Result values.

func AndThen3

func AndThen3[T any, U any](v T, err error, op func(T) (U, error)) Result[U]

AndThen3 calls op if the result is Ok, otherwise returns the error of self. This function can be used for control flow based on Result values.

func Assert

func Assert[T any, U any](o Result[T]) Result[U]

Assert asserts Result[T] as Result[U].

func Assert2

func Assert2[T any, U any](v T, err error) Result[U]

Assert2 asserts a value and an error as a Result[U].

Examples

var v = 1
var err = nil
var result = Assert2(v, err)
assert.Equal(t, core.Ok[int](1), result)

func AssertRet

func AssertRet[T any](i any) Result[T]

AssertRet returns the Result[T] of asserting `i` to type `T`

func Flatten

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

Flatten converts from Result[Result[T]] to Result[T].

Examples

var r1 = core.Ok(core.Ok(1))
var result1 = Flatten(r1)
assert.Equal(t, core.Ok[int](1), result1)
var r2 = core.Ok(core.TryErr[int](errors.New("error")))
var result2 = Flatten(r2)
assert.Equal(t, "error", result2.Err().Error())
var r3 = core.TryErr[Result[int]](errors.New("error"))
var result3 = Flatten(r3)
assert.Equal(t, "error", result3.Err().Error())

func Flatten2

func Flatten2[T any](r Result[T], err error) Result[T]

Flatten2 converts from `(Result[T], error)` to Result[T].

Examples

var r1 = core.Ok(1)
var err1 = nil
var result1 = Flatten2(r1, err1)
assert.Equal(t, core.Ok[int](1), result1)
var r2 = core.Ok(1)
var err2 = errors.New("error")
var result2 = Flatten2(r2, err2)
assert.Equal(t, "error", result2.Err().Error())
var r3 = core.TryErr[int](errors.New("error"))
var err3 = nil
var result3 = Flatten2(r3, err3)
assert.Equal(t, "error", result3.Err().Error())

func FmtErr

func FmtErr[T any](format string, args ...any) Result[T]

FmtErr wraps a failure result with a formatted error.

func Map

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

Map maps a Result[T] to Result[U] by applying a function to a contained Ok value, leaving an error untouched. This function can be used to compose the results of two functions.

func Map2

func Map2[T any, U any](v T, err error, f func(T) U) Result[U]

Map2 maps a value and an error as a Result[U] by applying a function to the value.

Examples

var v = 1
var err = nil
var result = Map2(v, err, func(v int) int { return v * 2 })
assert.Equal(t, core.Ok[int](2), result)

func Ok

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

Ok wraps a successful result.

func Ret

func Ret[T any](v T, err error) Result[T]

Ret wraps a result.

func TryErr

func TryErr[T any](err any) Result[T]

TryErr wraps a failure result. NOTE: If err is nil, TryErr(nil) returns Ok with zero value. This follows the principle that nil represents "no error", so TryErr(nil) should be Ok.

Example:

```go
result := result.TryErr[string](nil)  // This is an ok state
if result.IsOk() {
	fmt.Println("This will be printed")
}
```

func XAssert

func XAssert[U any](o Result[any]) Result[U]

XAssert asserts Result[any] as Result[U].

func XAssert2

func XAssert2[U any](v any, err error) Result[U]

XAssert2 asserts a value and an error as a Result[U].

Examples

var v = 1
var err = nil
var result = XAssert2(v, err)
assert.Equal(t, core.Ok[int](1), result)

type VoidResult

type VoidResult = core.VoidResult

VoidResult is an alias for core.VoidResult. VoidResult is a type alias for Result[Void], used for operations that only return success or failure without a value.

func OkVoid

func OkVoid() VoidResult

OkVoid returns Ok[Void](nil).

Example:

```go
var result result.VoidResult = result.OkVoid()
```

func RetVoid

func RetVoid(err error) VoidResult

RetVoid wraps an error as VoidResult (Result[Void]). Returns Ok[Void](nil) if maybeError is nil, otherwise returns Err[Void](maybeError).

Example:

```go
var result result.VoidResult = result.RetVoid(maybeError)
```

func TryErrVoid

func TryErrVoid(err any) VoidResult

TryErrVoid wraps a failure result as VoidResult. NOTE: If err is nil, TryErrVoid(nil) returns OkVoid().

Jump to

Keyboard shortcuts

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