Documentation
¶
Index ¶
- type Result
- func (r Result[T]) Expect(message string) T
- func (r Result[T]) IsErr() bool
- func (r Result[T]) IsOk() bool
- func (r Result[T]) String() string
- func (r *Result[T]) UnmarshalJSON(data []byte) error
- func (r Result[T]) Unwrap() T
- func (r Result[T]) UnwrapErr() error
- func (r Result[T]) UnwrapOr(value T) T
- func (r Result[T]) UnwrapOrElse(f func() T) T
- func (r Result[T]) UnwrapOrZero() T
- func (r Result[T]) Value() (T, error)
Examples ¶
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 failure or success. The Ok variant represents success and contains a value. The Err variant represent a failure and contains an error.
func (Result[T]) Expect ¶ added in v0.19.0
Expect returns the underlying value of an Ok variant, or panics with the provided message if called on an Err variant.
Example ¶
package main
import (
"fmt"
"github.com/BooleanCat/go-functional/result"
)
func main() {
fmt.Println(result.Ok(4).Expect("oops"))
}
Output: 4
func (Result[T]) IsErr ¶
IsErr returns true if the Result is an Err variant.
Example ¶
package main
import (
"errors"
"fmt"
"github.com/BooleanCat/go-functional/result"
)
func main() {
fmt.Println(result.Ok(4).IsErr())
fmt.Println(result.Err[int](errors.New("oops")).IsErr())
}
Output: false true
func (Result[T]) IsOk ¶
IsOk returns true if the Result is an Ok variant.
Example ¶
package main
import (
"errors"
"fmt"
"github.com/BooleanCat/go-functional/result"
)
func main() {
fmt.Println(result.Ok(4).IsOk())
fmt.Println(result.Err[int](errors.New("oops")).IsOk())
}
Output: true false
func (Result[T]) String ¶
String implements the fmt.Stringer interface.
func (*Result[T]) UnmarshalJSON ¶ added in v0.17.0
UnmarshalJSON implements the json.Unmarshaler interface. Values will be unmarshaled as Ok variants.
Example ¶
package main
import (
"encoding/json"
"fmt"
"github.com/BooleanCat/go-functional/result"
)
func main() {
var words result.Result[[]string]
_ = json.Unmarshal([]byte(`["Foo", "Bar"]`), &words)
fmt.Println(words)
}
Output: Ok([Foo Bar])
func (Result[T]) Unwrap ¶
func (r Result[T]) Unwrap() T
Unwrap returns the underlying value of an Ok variant, or panics if called on an Err variant.
Example ¶
package main
import (
"fmt"
"github.com/BooleanCat/go-functional/result"
)
func main() {
fmt.Println(result.Ok(4).Unwrap())
}
Output: 4
func (Result[T]) UnwrapErr ¶ added in v0.13.0
UnwrapErr returns the underlying error of an Err variant, or panics if called on an Ok variant.
Example ¶
package main
import (
"errors"
"fmt"
"github.com/BooleanCat/go-functional/result"
)
func main() {
err := result.Err[int](errors.New("oops")).UnwrapErr()
fmt.Println(err)
}
Output: oops
func (Result[T]) UnwrapOr ¶
func (r Result[T]) UnwrapOr(value T) T
UnwrapOr returns the underlying value of an Ok variant, or the provided value on an Err variant.
Example ¶
package main
import (
"errors"
"fmt"
"github.com/BooleanCat/go-functional/result"
)
func main() {
fmt.Println(result.Ok(4).UnwrapOr(3))
fmt.Println(result.Err[int](errors.New("oops")).UnwrapOr(3))
}
Output: 4 3
func (Result[T]) UnwrapOrElse ¶
func (r Result[T]) UnwrapOrElse(f func() T) T
UnwrapOrElse returns the underlying value of an Ok variant, or the result of calling the provided function on an Err variant.
Example ¶
package main
import (
"errors"
"fmt"
"github.com/BooleanCat/go-functional/result"
)
func main() {
fmt.Println(result.Ok(4).UnwrapOrElse(func() int {
return 3
}))
fmt.Println(result.Err[int](errors.New("oops")).UnwrapOrElse(func() int {
return 3
}))
}
Output: 4 3
func (Result[T]) UnwrapOrZero ¶
func (r Result[T]) UnwrapOrZero() T
UnwrapOrZero returns the underlying value of an Ok variant, or the zero value of an Err variant.
Example ¶
package main
import (
"errors"
"fmt"
"github.com/BooleanCat/go-functional/result"
)
func main() {
fmt.Println(result.Ok(4).UnwrapOrZero())
fmt.Println(result.Err[int](errors.New("oops")).UnwrapOrZero())
// Output
// 4
// 0
}
func (Result[T]) Value ¶
Value returns the underlying value and nil for an Ok variant, or the zero value and an error for an Err variant.
Example ¶
package main
import (
"fmt"
"github.com/BooleanCat/go-functional/result"
)
func main() {
value, ok := result.Ok(4).Value()
fmt.Println(value)
fmt.Println(ok)
}
Output: 4 <nil>