res

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2025 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package res provides a generic, Rust-inspired `Result[T]` type for expressive error handling.

Core Concept

A `Result[T]` is a type that represents either a success (containing a value of type T) or a failure (containing an error). It is a monadic type that allows for chaining operations in a clean, readable way, especially in data processing pipelines where each step can fail.

This pattern provides an alternative to returning `(value, error)` pairs at each step. Instead of checking for an error after every call, you can chain methods and handle the final result once.

Warning: Paradigm and Trade-offs

While powerful, the `Result` type introduces a paradigm that is not idiomatic Go. Standard Go error handling (returning `(T, error)`) is simpler and more direct for most use cases. The `Result` type is best suited for specific scenarios like complex data transformation chains.

Be especially cautious with methods like `Unwrap` and `Expect`, which panic on an `Err` value. They should only be used when an error is considered a fatal, unrecoverable bug, similar to the `must` package.

Example

// A function that returns a Result
func ParseInt(s string) res.Result[int] {
	n, err := strconv.Atoi(s)
	return res.Of(n, err)
}

// Chaining operations
result := ParseInt("123").Unwrap() // result is 123

// Safely handling the result
val, ok := ParseInt("not-a-number").Ok()
// val is 0, ok is false

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Or

func Or[T any](v T, err error, defaultValue T) T

Or is a utility function that simplifies handling of (value, error) returns. It returns the value if err is nil, otherwise it returns the provided default value.

func OrZero

func OrZero[T any](v T, err error) T

OrZero is a utility function that simplifies handling of (value, error) returns. It returns the value if err is nil, otherwise it returns the zero value of the type.

Types

type Result

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

Result is a type that represents either a success (containing a value of type T) or a failure (containing an error). It is a monadic type that allows for chaining operations in a clean, readable way. See the package documentation for more details and usage examples.

func Err

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

Err creates a new failed Result containing the given error.

func Of

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

Of converts a standard Go (value, error) pair into a Result. If err is not nil, it returns an Err result; otherwise, it returns an Ok result.

func Ok

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

Ok creates a new successful Result containing the given value.

func (Result[T]) Err

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

Err returns the contained error, or nil if the result is Ok.

func (Result[T]) Expect

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

Expect returns the contained Ok value. It panics with a custom message if the result is an Err. This is similar to Unwrap but provides a more context-specific panic message.

func (Result[T]) IsErr

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

IsErr returns true if the result is an Err (i.e., contains an error).

func (Result[T]) IsOk

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

IsOk returns true if the result is Ok (i.e., does not contain an error).

func (Result[T]) Ok

func (r Result[T]) Ok() (T, bool)

Ok returns the contained value and a boolean indicating if the result was Ok. This provides a safe, idiomatic Go way to access the value.

func (Result[T]) Unwrap

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

Unwrap returns the contained Ok value. It panics if the result is an Err. Because this function may panic, it should only be used when the caller is certain that the result is Ok, or when a panic is the desired behavior. See also: Expect, UnwrapOr.

func (Result[T]) UnwrapOr

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

UnwrapOr returns the contained Ok value or a provided default value. It is a safe way to access the value without panicking.

Jump to

Keyboard shortcuts

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