Guides
Result and error propagation
Chain Go functions that return errors, recover when needed, and add context or transform values.
Chain validation steps
ResultOf wraps the first parse result. Result.AndThen accepts an ordinary Go function and calls it if the previous step succeeds. Invalid integers retain the parsing error, while valid integers proceed to port range validation.
// parsePort parses an integer, then checks its range; when a step fails, the
// following steps are skipped. AndThen takes a (value, error) function as is.
func parsePort(s string) fp.Result[int] {
return fp.ResultOf(strconv.Atoi(s)).AndThen(checkPort)
}Recovery and conversion
Result.Recover calls a recovery function only when the current result contains an error. It can supply a fallback port if parsing fails. Result.MapErr attaches operation context to make it easier to identify which step failed.
// Recover runs only on failure and can supply a fallback; MapErr adds
// context to the error.
port := parsePort("http").Recover(func(error) (int, error) { return 8080, nil })
fmt.Println("fallback port:", port.MustGet())
_, err := parsePort("70000").MapErr(func(err error) error {
return fmt.Errorf("read PORT: %w", err)
}).Get()
fmt.Println("error:", err)Result.Map can change the value type on success. Use Result.Get to pass the final value and error back to a Go API.
// Map is a generic method, so a Result[int] becomes a Result[string]; Get
// returns to the plain (value, error) form.
addr, err := parsePort("443").Map(func(n int) string { return ":" + strconv.Itoa(n) }).Get()
fmt.Println("listen address:", addr, err)