result

package module
v0.3.1 Latest Latest
Warning

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

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

README

go-result

Generic Result type for Go — Ok[T] / Err[T] with mapping and chaining.

Installation

go get github.com/philiprehberger/go-result

Usage

Basic Result
import "github.com/philiprehberger/go-result"

r := result.Ok(42)
fmt.Println(r.Unwrap()) // 42

r2 := result.Err[int](errors.New("not found"))
fmt.Println(r2.UnwrapOr(0)) // 0
UnwrapOrElse — Compute Default from Error
val := r.UnwrapOrElse(func(err error) int {
    log.Printf("falling back due to: %v", err)
    return -1
})
Expect — Unwrap with Custom Panic Message
cfg := r.Expect("config must be valid")
// panics with "config must be valid: <error>" if Err
Or — Fallback Result
r := result.Err[int](errors.New("fail"))
val := r.Or(result.Ok(42)) // Ok(42)
String Representation
fmt.Println(result.Ok(42))              // Ok(42)
fmt.Println(result.Err[int](err))       // Err(something failed)

Note: Unwrap() and Expect() panic if the Result is an error. Use UnwrapOr or UnwrapOrElse for safe extraction.

Try — Wrap (T, error)
r := result.Try(func() (int, error) {
    return strconv.Atoi("42")
})
// Ok(42)
Map and FlatMap
r := result.Ok(10)
doubled := result.Map(r, func(v int) int { return v * 2 })
// Ok(20)

chained := result.FlatMap(r, func(v int) result.Result[string] {
    return result.Ok(fmt.Sprintf("value: %d", v))
})
Match
msg := result.Match(r,
    func(v int) string { return fmt.Sprintf("got %d", v) },
    func(err error) string { return fmt.Sprintf("error: %v", err) },
)
Collect Results
results := []result.Result[int]{result.Ok(1), result.Ok(2), result.Ok(3)}
combined := result.All(results) // Ok([1, 2, 3])

License

MIT

Documentation

Overview

Package result provides a generic Result type for Go inspired by Rust's Result<T, E>.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Match

func Match[T any, U any](r Result[T], onOk func(T) U, onErr func(error) U) U

Match applies one of two functions depending on whether the Result is Ok or Err.

Types

type Result

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

Result represents either a success value (Ok) or an error value (Err).

func All

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

All collects a slice of Results into a Result containing a slice of values. Returns the first error encountered.

func Err

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

Err creates a failed Result containing the given error.

func Errf

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

Errf creates a failed Result with a formatted error message.

func FlatMap

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

FlatMap transforms the success value using a function that returns a Result.

func Map

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

Map transforms the success value using the given function.

func Ok

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

Ok creates a successful Result containing the given value.

func Try

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

Try wraps a function call that returns (T, error) into a Result.

func (Result[T]) Error

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

Error returns the error value or nil if the Result is Ok.

func (Result[T]) Expect added in v0.3.0

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

Expect returns the success value or panics with the given message if the Result is an error.

func (Result[T]) IsErr

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

IsErr returns true if the Result is an error.

func (Result[T]) IsOk

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

IsOk returns true if the Result is a success.

func (Result[T]) Or added in v0.3.0

func (r Result[T]) Or(other Result[T]) Result[T]

Or returns the Result if it is Ok, otherwise returns the provided fallback Result.

func (Result[T]) String added in v0.3.0

func (r Result[T]) String() string

String returns a human-readable representation of the Result.

func (Result[T]) Unwrap

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

Unwrap returns the success value or panics if the Result is an error.

func (Result[T]) UnwrapOr

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

UnwrapOr returns the success value or the provided default.

func (Result[T]) UnwrapOrElse

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

UnwrapOrElse returns the success value or calls the provided function.

Jump to

Keyboard shortcuts

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