anygo

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2025 License: MIT Imports: 1 Imported by: 0

README

anygo

anygo is an idiomatic Go library inspired by Rust's Result<T, E> type. It enables safe and expressive error handling without relying on traditional error-only returns.

Installation

go get github.com/daxartio/anygo

Overview

Core type:

type Result[T any]

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

Basic Usage

import (
    "errors"
    "fmt"

    "github.com/daxartio/anygo"
)

func compute() anygo.Result[int] {
    if success {
        return anygo.Ok(42)
    }
    return anygo.Err[int](errors.New("something went wrong"))
}

res := compute()
if res.IsOk() {
    val := res.MustUnwrap()
    fmt.Println("Value:", val)
} else {
    fmt.Println("Error occurred")
}

API

Creation
  • Ok(value T) Result[T] — creates a successful result.
  • Err[T](err error) Result[T] — creates a failed result.
Inspection
  • IsOk() bool — true if result is Ok.
  • IsErr() bool — true if result is Err.
Unwrapping
  • Unwrap() (T, error) — returns value and error.
  • UnwrapOr(default T) T — value or default if Err.
  • UnwrapOrElse(func() T) T — value or result of fallback function.
  • MustUnwrap() T — panics if Err.
  • Expect(msg string) T — panics with message if Err.
Combinators
  • Map(Result[T], func(T) U) Result[U] — transforms value.
  • MapErr(func(error) error) Result[T] — transforms error.
  • AndThen(Result[T], func(T) Result[U]) Result[U] — chains computations.
  • Inspect(func(T)) Result[T] — performs side effect if Ok.
  • Or(Result[T]) Result[T] — fallback result if Err.
  • OrElse(func() Result[T]) Result[T] — fallback result from function.
  • ToPtr() *T — pointer to value or nil.

License

MIT

Documentation

Index

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 AndThen

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

func Err

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

Err returns a failed Result containing an error.

Example:

r := anygo.Err[string](errors.New("oops"))
fmt.Println(r.IsErr()) // true

func Map

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

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

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

Ok returns a successful Result containing value.

Example:

r := anygo.Ok("hello")
fmt.Println(r.IsOk()) // true

func (Result[T]) Errorf added in v1.1.0

func (r Result[T]) Errorf(format string, a ...any) Result[T]

Errorf adds context to the error if Result is Err.

func (Result[T]) Expect

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

Expect panics with the provided message if Result is Err.

func (Result[T]) Inspect

func (r Result[T]) Inspect(f func(T)) Result[T]

Inspect calls a function on the value if Result is Ok.

func (Result[T]) IsErr

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

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

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

IsOk returns true if the Result has no error.

Example:

r := anygo.Ok(123)
fmt.Println(r.IsOk()) // true

func (Result[T]) Map

func (r Result[T]) Map(f func(T) T) Result[T]

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]) MapErr

func (r Result[T]) MapErr(f func(error) error) Result[T]

MapErr transforms the error if present.

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]) Or

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

Or returns self if Ok, otherwise returns the alternative.

func (Result[T]) OrElse

func (r Result[T]) OrElse(f func() Result[T]) Result[T]

OrElse calls the fallback function if Err.

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

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

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

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

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"

Jump to

Keyboard shortcuts

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