Documentation ¶
Index ¶
- Variables
- func Catch[T any](res *Result[T])
- func CatchError(errorPtr *error)
- func Must[T any](val T, err error) T
- type Result
- func Apply[T any, U any](from Result[T], f func(T) U) Result[U]
- func ApplyE[T any, U any](from Result[T], f func(T) (U, error)) Result[U]
- func ApplyResult[T any, U any](from Result[T], f func(T) Result[U]) Result[U]
- func Deref[T any](res Result[*T]) Result[T]
- func Err[T any](err error) Result[T]
- func MapE[T any, U any](slice []T, f func(T) (U, error)) []Result[U]
- func MapR[T any, U any](slice []T, f func(T) Result[U]) []Result[U]
- func MarshalIndentJson[T any](val T, prefix string, indent string) Result[json.RawMessage]
- func MarshalJson[T any](val T) Result[json.RawMessage]
- func Ptr[T any](res *Result[T]) Result[*T]
- func UnmarshalJson[T any](data []byte) Result[T]
- func UnmarshalJsonString[T any](data string) Result[T]
- func Val[T any](value T) Result[T]
- func Void(err error) Result[_Void]
- func Wrap[T any](val T, err error) Result[T]
- func (self Result[T]) Err() error
- func (self Result[T]) ErrOr(other error) error
- func (self Result[T]) Expect(msg string) T
- func (self Result[T]) ExpectError(msg string) error
- func (self Result[T]) ExpectErrorf(format string, a ...any) error
- func (self Result[T]) Expectf(format string, a ...any) T
- func (self Result[T]) IsError() bool
- func (self Result[T]) IsErrorAnd(cond func(error) bool) bool
- func (self Result[T]) IsValue() bool
- func (self Result[T]) IsValueAnd(cond func(T) bool) bool
- func (self Result[T]) Must() T
- func (self Result[T]) Mustf(format string, a ...any) T
- func (self Result[T]) String() string
- func (self Result[T]) Unwrap() T
- func (self Result[T]) UnwrapOr(valueIfError T) T
- func (self Result[T]) UnwrapOrDefault() T
- func (self Result[T]) UnwrapOrElse(f func(error) T) T
- func (self Result[T]) UnwrapUnsafe() T
- func (self Result[T]) UnwrapWithError() (T, error)
- type ResultVoid
Constants ¶
This section is empty.
Variables ¶
var ErrDerefNil = errors.New("derefencing nil")
Functions ¶
func CatchError ¶
func CatchError(errorPtr *error)
Defer Catch(&err) to convert failed invocation of Must into error
Types ¶
type Result ¶
type Result[T any] struct { // contains filtered or unexported fields }
A value or an error
func ApplyE ¶
Applies f to the stored value or keeps error unchanged. If f returns an error, set the error
func ApplyResult ¶
Applies f to the stored value or keeps error unchanged. If f returns an error, set the error
func MarshalIndentJson ¶ added in v0.2.3
Marshal a value of the type T into indented JSON or error
func MarshalJson ¶ added in v0.2.3
func MarshalJson[T any](val T) Result[json.RawMessage]
Marshal a value of the type T into JSON or error
func UnmarshalJson ¶
Unmarshal JSON data into a value of the type T or error
func UnmarshalJsonString ¶
Unmarshal JSON string into a value of the type T or error
func (Result[T]) ExpectError ¶
Returns the error or panics with the given message
func (Result[T]) ExpectErrorf ¶
Returns the error or panics with the given formatted message
func (Result[T]) IsErrorAnd ¶
True if self is an error and it matches the condition
func (Result[T]) IsValueAnd ¶
True is self contains a value and it matches the condition
func (Result[T]) Must ¶
func (self Result[T]) Must() T
Extracts the stored value or panics with a catchable value.
func (Result[T]) Mustf ¶
Extracts the stored value or panics with a catchable string.
The string is appended with 'error: <error's Error() method result>'
func (Result[T]) String ¶
Builds a string representation of the result object. If it is an error, returns the err.Error() string If T has String method, calls String If T has ToString method, calls ToString If T has MarshalText method, calls MarshalText Otherwise calls fmt.Sprint(self.Unwrap())
func (Result[T]) UnwrapOr ¶
func (self Result[T]) UnwrapOr(valueIfError T) T
Returns the stored value or the provided default value
func (Result[T]) UnwrapOrDefault ¶
func (self Result[T]) UnwrapOrDefault() T
Returns the stored value or the default value for the type T
func (Result[T]) UnwrapOrElse ¶
Returns the stored value or transforms the error
func (Result[T]) UnwrapUnsafe ¶
func (self Result[T]) UnwrapUnsafe() T
Returns the stored value without checking the error
func (Result[T]) UnwrapWithError ¶
Converts to the pair (value, error)
type ResultVoid ¶
type ResultVoid = Result[_Void]
func NoError ¶
func NoError(err error) ResultVoid
In the case of error panics with the catchable value.