Skip to content

Result[T]

This generic type tries to mimic the Result type from other languages. It wraps a value/error pair, and provides a set of methods to resolve/unwrap underlying value with a specific value/error handling.

Usage:

go
// We can use Wrap function to wrap a value/error pair into Result[T] type
res := errx.Wrap(strconv.Atoi("42"))

// Getter methods to get value or error
val, err := res.Value(), res.Error()

// Getter for getting value or default value if error is present
val := res.ValueOr(-1)

// Getter for getting value or panicking if error is present
val := res.Must()

// Call handlers to execute functions based on error presence
res.Then(func(val int) {
	fmt.Println(val)
}).Catch(func(err error) {
	fmt.Println("Error:", err)
})

// The main point of handlers is using shared functions to handle different cases
res.Then(processValue).Catch(logError)
// or
val := res.Catch(logError).ValueOr(-1)