Documentation
¶
Overview ¶
Package result provides a Result type representing either a successful value or an error.
Index ¶
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 holds either a successful value of type T or an error.
func FlatMap ¶
FlatMap applies fn to the value inside r and returns the resulting Result. If r is an error, FlatMap returns an error Result of type R.
func Map ¶
Map applies fn to the value inside r and returns a new Result containing the result. If r is an error, Map returns an error Result of type R.
Example ¶
package main
import (
"fmt"
"github.com/mikehelmick/go-functional/result"
)
func main() {
r := result.Map(result.OK(21), func(v int) int { return v * 2 })
fmt.Println(r.MustGet())
}
Output: 42
func OK ¶
OK returns a successful Result containing v.
Example ¶
package main
import (
"fmt"
"github.com/mikehelmick/go-functional/result"
)
func main() {
r := result.OK(42)
if v, err := r.Get(); err == nil {
fmt.Println(v)
}
}
Output: 42
func Of ¶
Of wraps the idiomatic Go (value, error) pair into a Result.
Example ¶
package main
import (
"fmt"
"strconv"
"github.com/mikehelmick/go-functional/result"
)
func main() {
r := result.Of(strconv.Atoi("123"))
fmt.Println(r.MustGet())
}
Output: 123
func (Result[T]) Get ¶
Get returns the value and nil error on success, or the zero value of T and the error on failure.