result

package
v3.5.4 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2025 License: AGPL-3.0 Imports: 2 Imported by: 0

Documentation

Overview

Package result provides utilities for dealing with iterators that can fail during iteration.

Result is useful to make it harder for callers to ignore errors. Using iter.Seq2[V, error] can make it easy to accidentally ignore errors:

func myIter() iter.Seq2[V, error] { ... }

func main() {
  for v := range myIter() { /* errors are ignored! */ }
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Collect

func Collect[V any](seq Seq[V]) ([]V, error)

Collect collects values from seq into a new slice and returns it. Any errors from seq are joined and returned as the second value.

func Pull

func Pull[V any](seq Seq[V]) (next func() (Result[V], bool), stop func())

Pull converts the "push-style" Result iterator sequence seq into a "pull-style" iterator accessed by the two functions next and stop.

Pull is a wrapper around iter.Pull.

Types

type Result

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

Result is a type used for representing a result from an operation that can fail.

func Error

func Error[V any](err error) Result[V]

Error returns a failed result with the given error.

func Value

func Value[V any](v V) Result[V]

Value returns a successful result with the given value.

func (Result[V]) Err

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

Err returns r's error, if any.

func (Result[V]) MustValue

func (r Result[V]) MustValue() V

MustValue returns r's value. If r is an error, MustValue panics.

func (Result[V]) Value

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

Value returns r's value and error.

type Seq

type Seq[V any] func(yield func(Result[V]) bool)

Seq is an iterator over sequences of result values. When called as seq(yield), seq calls yield(r) for each value r in the sequence, stopping early if yield returns false.

See the iter package for more information on iterators.

func Iter

func Iter[V any](seq func(yield func(V) bool) error) Seq[V]

Iter produces a new Seq[V] from a given function that can fail. Values passed to yield are wrapped in a call to Value, while a non-nil error is wrapped in a call to Error.

Iter makes it easier to write failable iterators and removes the need to manually wrap values and errors into a Result.

Jump to

Keyboard shortcuts

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