Documentation
¶
Index ¶
- type Result
- func (r Result[T]) Errorf(format string, a ...any) Result[T]
- func (r Result[T]) Expect(msg string) T
- func (r Result[T]) Inspect(f func(T)) Result[T]
- func (r Result[T]) IsErr() bool
- func (r Result[T]) IsOk() bool
- func (r Result[T]) Map(f func(T) T) Result[T]
- func (r Result[T]) MapErr(f func(error) error) Result[T]
- func (r Result[T]) MustUnwrap() T
- func (r Result[T]) Or(other Result[T]) Result[T]
- func (r Result[T]) OrElse(f func() Result[T]) Result[T]
- func (r Result[T]) ToPtr() *T
- func (r Result[T]) Unwrap() (T, error)
- func (r Result[T]) UnwrapError() error
- func (r Result[T]) UnwrapOr(def T) T
- func (r Result[T]) UnwrapOrElse(f func() T) T
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Result ¶
type Result[T any] struct { // contains filtered or unexported fields }
Result represents a value of type T or an error.
func Err ¶
Err returns a failed Result containing an error.
Example:
r := anygo.Err[string](errors.New("oops"))
fmt.Println(r.IsErr()) // true
func Map ¶
Map applies a function to the value if ok, propagates error otherwise.
Example:
r := anygo.Ok(2)
squared := anygo.Map(r, func(x int) int { return x * x })
fmt.Println(squared.MustUnwrap()) // 4
func Ok ¶
Ok returns a successful Result containing value.
Example:
r := anygo.Ok("hello")
fmt.Println(r.IsOk()) // true
func (Result[T]) IsErr ¶
IsErr returns true if the Result has an error.
Example:
r := anygo.Err[int](errors.New("fail"))
fmt.Println(r.IsErr()) // true
func (Result[T]) IsOk ¶
IsOk returns true if the Result has no error.
Example:
r := anygo.Ok(123) fmt.Println(r.IsOk()) // true
func (Result[T]) Map ¶
Map applies a function to the value if ok, propagates error otherwise.
Example:
r := anygo.Ok(2)
squared := r.Map(func(x int) int { return x * x })
func (Result[T]) MustUnwrap ¶
func (r Result[T]) MustUnwrap() T
MustUnwrap returns the value or panics if there's an error.
Example:
r := anygo.Ok("safe")
fmt.Println(r.MustUnwrap()) // "safe"
// anygo.Err[string](errors.New("boom")).MustUnwrap() // panics
func (Result[T]) ToPtr ¶
func (r Result[T]) ToPtr() *T
ToPtr returns a pointer to the value if Ok, or nil if Err.
func (Result[T]) Unwrap ¶
Unwrap returns the value and error.
Example:
r := anygo.Ok("hi")
v, err := r.Unwrap()
fmt.Println(v, err) // "hi", nil
func (Result[T]) UnwrapError ¶ added in v1.1.0
UnwrapError returns the error if present, or nil if ok.
func (Result[T]) UnwrapOr ¶
func (r Result[T]) UnwrapOr(def T) T
UnwrapOr returns the value if ok, or the default otherwise.
Example:
r := anygo.Err[string](errors.New("fail"))
fmt.Println(r.UnwrapOr("default")) // "default"
func (Result[T]) UnwrapOrElse ¶
func (r Result[T]) UnwrapOrElse(f func() T) T
UnwrapOrElse returns the value if ok, or calls the fallback function otherwise.
Example:
r := anygo.Err[string](errors.New("fail"))
fmt.Println(r.UnwrapOrElse(func() string { return "fallback" })) // "fallback"