Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Result ¶
type Result[T any, E comparable] struct { // contains filtered or unexported fields }
Result of an operation.
Designed to replace the return pattern of (value, error). Result is either a value or an error.
Example ¶
exampleFn := func(success bool) result.Result[int, FailureError] {
if success {
return result.Ok[int, FailureError](1)
}
return result.Err[int](errFailureError)
}
// Success result.
fmt.Println(exampleFn(true).IsOk())
fmt.Println(exampleFn(true).Error())
fmt.Println(exampleFn(true).Value())
fmt.Println(exampleFn(true).ValueOr(2))
fmt.Println(exampleFn(true).ValueOrPanic())
// Error result.
fmt.Println(exampleFn(false).IsOk())
fmt.Println(exampleFn(false).Error())
fmt.Println(exampleFn(false).Value())
fmt.Println(exampleFn(false).ValueOr(2))
//fmt.Println(exampleFn(false).ValueOrPanic()) // Will panic
Output: true none 1 1 1 false failure 0 2
func (Result[T, E]) Error ¶
func (self Result[T, E]) Error() E
Error of the result.
The Error value can never be `nil`, so it is recommended to use Result.IsOk(). Another alternative is to call Result.Error().IsNone().
func (Result[T, E]) Result ¶
func (self Result[T, E]) Result() (T, E)
Result decomposes into the basic (T, error) return value.
Useful when decomposing into variables for custom evaluation.
func (Result[T, E]) Value ¶
func (self Result[T, E]) Value() T
Value of the Ok result.
Note: If called on an error result, this will be the zero value of T.
func (Result[T, E]) ValueOr ¶
func (self Result[T, E]) ValueOr(defaultValue T) T
ValueOr default value if not an Ok result.
func (Result[T, E]) ValueOrPanic ¶
func (self Result[T, E]) ValueOrPanic() T
ValueOrPanic if not an Ok result.
It is recommended to only call this during app initialization. Otherwise, use Result.ValueOr().