Documentation
¶
Overview ¶
Package results provides generic types and functions for handling function results.
This package is designed to be simple, efficient, and easy to use while adhering to Go's idiomatic practices.
The package aims to reduce boilerplate code associated with error checking and fallback value assignment. It is particularly useful for functions that return multiple values and an error.
May ¶
Maybe1, Maybe2, and Maybe3 types encapsulate one, two, or three values respectively, along with an error. These types simplify error handling and provide a convenient way to work with functions that may fail.
May1, May2 and May3 functions return a value of these types.
The number "1", "2" and "3" in the function name refer to the number of values excluding an error they return.
Example
value := results.May1(someFunction()).Or(defaultValue) v1, v2 := results.May2(anotherFunction()).Or(default1, default2)
Must ¶
Must1, Must2 and Must3 functions panic if an error occurs, which is useful for operations that should never fail during normal execution.
The number "1", "2" and "3" in the function name refer to the number of values they return.
Example
value := results.Must1(someFunction()) v1, v2 := results.Must2(anotherFunction())
Multiple Results ¶
Functions allow you to select specific parameters from a set of two or three parameters of different types.
- Get1of2 returns the first parameter from two parameters
- Get2of2 returns the second parameter from two parameters
- Get1of3 returns the first parameter from three parameters
- Get2of3 returns the second parameter from three parameters
- Get3of3 returns the third parameter from three parameters
- Get12of3 returns the first two parameters from three parameters
- Get23of3 returns the last two parameters from three parameters
These deprecate Take2, Drop2, Take3 and Drop3.
Example
first := results.jet1of2(1, "two") second := results.Get2of2(1, "two") firstOfThree := results.Get1of3(1, "two", 3.0)
If, IfFunc, If1 and If2 ¶
If, IfFunc, If1 and If2 functions simulate conditional expression.
Example
// someFunction returns a value of any type and a boolean value. value := results.If1(someFunction()).Or(valueForFalse) // anotherFunction returns two values of any type and a boolean value. v1, v2 := results.If2(anotherFunction()).Or(fallback0, fallback1)
ErrorAs ¶
The coding pattern using errors.As is following:
var pathError *fs.PathError
if errors.As(err, &pathError) {
Using ErrorAs is a bit more concise:
if pathError, ok := results.ErrorAs[*fs.PathError](err); ok {
Improvements:
- Just one line.
- Create a scope for the pathError variable.
TypeAs ¶
TypeAs returns the result of type assertions. This is a funciton, so it is addressable.
Index ¶
- func Drop2[T0, T1 any](_ T0, b T1) T1deprecated
- func Drop3[T0, T1, T2 any](_ T0, b T1, c T2) (T1, T2)deprecated
- func ErrorAs[T any](err error) (target T, ok bool)
- func Get12of3[A, B, C any](a A, b B, _ C) (A, B)
- func Get1of2[A, B any](a A, _ B) A
- func Get1of3[A, B, C any](a A, _ B, _ C) A
- func Get23of3[A, B, C any](_ A, b B, c C) (B, C)
- func Get2of2[A, B any](_ A, b B) B
- func Get2of3[A, B, C any](_ A, b B, _ C) B
- func Get3of3[A, B, C any](_ A, _ B, c C) C
- func If[T any](ok bool, true, false T) T
- func IfFunc[T any](ok bool, true, false func() T) T
- func Must(err error)
- func Must1[T any](v T, err error) T
- func Must2[T0, T1 any](v0 T0, v1 T1, err error) (T0, T1)
- func Must3[T0, T1, T2 any](v0 T0, v1 T1, v2 T2, err error) (T0, T1, T2)
- func Take2[T0, T1 any](a T0, _ T1) T0deprecated
- func Take3[T0, T1, T2 any](a T0, _ T1, _ T2) T0deprecated
- func TypeAs[T any](v any) (value T, ok bool)
- type Maybe
- type Maybe1
- type Maybe2
- type Maybe3
- func (m Maybe3[T0, T1, T2]) Error() error
- func (m Maybe3[T0, T1, T2]) HasError() bool
- func (m Maybe3[T0, T1, T2]) Or(d0 T0, d1 T1, d2 T2) (T0, T1, T2)
- func (m Maybe3[T0, T1, T2]) OrFunc(f func() (T0, T1, T2)) (T0, T1, T2)
- func (m Maybe3[T0, T1, T2]) OrPanic() (T0, T1, T2)
- func (m Maybe3[T0, T1, T2]) Values() []any
- type True
- type True1
- type True2
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Drop2
deprecated
added in
v1.4.0
func Drop2[T0, T1 any](_ T0, b T1) T1
Drop2 discards the first of the two provided arguments and returns the second.
Deprecated: use Get2of2 instead.
Example ¶
package main
import (
"fmt"
"net"
"github.com/goaux/results"
)
func main() {
fmt.Println(results.Drop2(
results.May2(net.SplitHostPort("example.com:80")).Or("localhost", "80"),
))
}
Output: 80
func Drop3
deprecated
added in
v1.4.0
func Drop3[T0, T1, T2 any](_ T0, b T1, c T2) (T1, T2)
Drop3 discards the first of the three provided arguments and returns the remaining two.
Deprecated: use Get23of3 instead.
Example ¶
package main
import (
"fmt"
"github.com/goaux/results"
)
func main() {
example := func() (x, y, z int) { return 11, 22, 33 }
fmt.Println(results.Drop3(example()))
}
Output: 22 33
func ErrorAs ¶ added in v1.6.0
ErrorAs returns the result of calling errors.As(err, &target).
The coding pattern using errors.As is following:
var pathError *fs.PathError
if errors.As(err, &pathError) {
Using ErrorAs is a bit more concise:
if pathError, ok := results.ErrorAs[*fs.PathError](err); ok {
Improvements:
- Just one line.
- Create a scope for the pathError variable.
Example ¶
package main
import (
"fmt"
"io/fs"
"github.com/goaux/results"
)
func main() {
err := fmt.Errorf("wrap %w", &fs.PathError{Op: "example"})
if pathError, ok := results.ErrorAs[*fs.PathError](err); ok {
fmt.Println(pathError.Op)
}
}
Output: example
func Get12of3 ¶ added in v1.7.0
func Get12of3[A, B, C any](a A, b B, _ C) (A, B)
Get12of3 returns the first two parameters of types A and B from three parameters.
func Get1of2 ¶ added in v1.7.0
func Get1of2[A, B any](a A, _ B) A
Get1of2 returns the first parameter of type A from two parameters.
Example ¶
package main
import (
"fmt"
"net"
"github.com/goaux/results"
)
func main() {
fmt.Println(results.Get1of2(
results.May2(net.SplitHostPort("example.com:80")).Or("localhost", "80"),
))
}
Output: example.com
func Get1of3 ¶ added in v1.7.0
func Get1of3[A, B, C any](a A, _ B, _ C) A
Get1of3 returns the first parameter of type A from three parameters.
Example ¶
package main
import (
"fmt"
"github.com/goaux/results"
)
func main() {
example := func() (x, y, z int) { return 11, 22, 33 }
fmt.Println(results.Get1of3(example()))
}
Output: 11
func Get23of3 ¶ added in v1.7.0
func Get23of3[A, B, C any](_ A, b B, c C) (B, C)
Get23of3 returns the last two parameters of types B and C from three parameters.
Example ¶
package main
import (
"fmt"
"github.com/goaux/results"
)
func main() {
example := func() (x, y, z int) { return 11, 22, 33 }
fmt.Println(results.Get23of3(example()))
}
Output: 22 33
func Get2of2 ¶ added in v1.7.0
func Get2of2[A, B any](_ A, b B) B
Get2of2 returns the second parameter of type B from two parameters.
Example ¶
package main
import (
"fmt"
"net"
"github.com/goaux/results"
)
func main() {
fmt.Println(results.Get2of2(
results.May2(net.SplitHostPort("example.com:80")).Or("localhost", "80"),
))
}
Output: 80
func Get2of3 ¶ added in v1.7.0
func Get2of3[A, B, C any](_ A, b B, _ C) B
Get2of3 returns the second parameter of type B from three parameters.
func Get3of3 ¶ added in v1.7.0
func Get3of3[A, B, C any](_ A, _ B, c C) C
Get3of3 returns the third parameter of type C from three parameters.
func If ¶ added in v1.8.0
If returns the true value if ok is true, otherwise the false value.
Example ¶
package main
import (
"fmt"
"github.com/goaux/results"
)
func main() {
fmt.Println(results.If(true, 42, 0))
}
Output: 42
func IfFunc ¶ added in v1.8.0
IfFunc returns the result of calling true function if ok is true, otherwise the result of calling false function.
Example ¶
package main
import (
"fmt"
"github.com/goaux/results"
)
func main() {
fmt.Println(
results.IfFunc(
true,
func() string { return "42" },
func() string { return "" },
),
)
}
Output: 42
func Must ¶ added in v1.3.0
func Must(err error)
Must panics if the error is non-nil. It's useful for operations that should never fail during normal execution.
func Must1 ¶ added in v1.1.0
Must1 panics if the error is non-nil, otherwise returns the value. It's useful for operations that should never fail during normal execution.
func Must2 ¶ added in v1.1.0
Must2 panics if the error is non-nil, otherwise returns the two values. It's useful for operations that should never fail during normal execution.
func Must3 ¶ added in v1.1.0
Must3 panics if the error is non-nil, otherwise returns the three values. It's useful for operations that should never fail during normal execution.
func Take2
deprecated
added in
v1.4.0
func Take2[T0, T1 any](a T0, _ T1) T0
Take2 returns the first of the two provided arguments, discarding the second.
Deprecated: use Get1of2 instead.
Example ¶
package main
import (
"fmt"
"net"
"github.com/goaux/results"
)
func main() {
fmt.Println(results.Take2(
results.May2(net.SplitHostPort("example.com:80")).Or("localhost", "80"),
))
}
Output: example.com
func Take3
deprecated
added in
v1.4.0
func Take3[T0, T1, T2 any](a T0, _ T1, _ T2) T0
Take3 returns the first of the three provided arguments, discarding the other two.
Deprecated: use Get1of3 instead.
Example ¶
package main
import (
"fmt"
"github.com/goaux/results"
)
func main() {
example := func() (x, y, z int) { return 11, 22, 33 }
fmt.Println(results.Take3(example()))
}
Output: 11
func TypeAs ¶ added in v1.9.0
TypeAs returns the result of type assertions. This is a function, so it is addressable.
value, ok := v.(T)
Example ¶
package main
import (
"fmt"
"github.com/goaux/results"
)
func main() {
asStr := results.TypeAs[string]
asInt := results.TypeAs[int]
fmt.Println(asStr("hello"))
fmt.Println(asInt("hello"))
}
Output: hello true 0 false
Types ¶
type Maybe ¶
type Maybe1 ¶
Maybe1 holds a value and an error
Example ¶
package main
import (
"fmt"
"strconv"
"github.com/goaux/results"
)
var _ results.Maybe = results.Maybe1[any]{}
func main() {
v := results.May1(strconv.Atoi("999")).Or(42)
fmt.Println(v)
v = results.May1(strconv.Atoi("999")).OrPanic()
fmt.Println(v)
v = results.May1(strconv.Atoi("NaN")).Or(42)
fmt.Println(v)
v = results.May1(strconv.Atoi("NaN")).OrFunc(func() int { return 80 })
fmt.Println(v)
}
Output: 999 999 42 80
func (Maybe1[T]) Or ¶
func (m Maybe1[T]) Or(defaultValue T) T
Or returns the default value if there's an error, otherwise returns the stored value
func (Maybe1[T]) OrFunc ¶ added in v1.2.0
func (m Maybe1[T]) OrFunc(f func() T) T
OrFunc returns the result of the function if there is an error, otherwise it returns the stored value. The function f is called only if there is an error.
type Maybe2 ¶
Maybe2 holds two values and an error
Example ¶
package main
import (
"fmt"
"net"
"github.com/goaux/results"
)
var _ results.Maybe = results.Maybe2[any, any]{}
func main() {
host, port := results.May2(net.SplitHostPort("example.com:80")).Or("localhost", "80")
fmt.Println(host, port)
host, port = results.May2(net.SplitHostPort("example.com:80")).OrPanic()
fmt.Println(host, port)
host, port = results.May2(net.SplitHostPort("error")).Or("localhost", "80")
fmt.Println(host, port)
host, port = results.May2(net.SplitHostPort("error")).OrFunc(
func() (string, string) { return "example.com", "8080" },
)
fmt.Println(host, port)
}
Output: example.com 80 example.com 80 localhost 80 example.com 8080
func (Maybe2[T0, T1]) Or ¶
func (m Maybe2[T0, T1]) Or(d0 T0, d1 T1) (T0, T1)
Or returns the default values if there's an error, otherwise returns the stored values
func (Maybe2[T0, T1]) OrFunc ¶ added in v1.2.0
func (m Maybe2[T0, T1]) OrFunc(f func() (T0, T1)) (T0, T1)
OrFunc returns the result of the function if there is an error, otherwise it returns the stored value. The function f is called only if there is an error.
type Maybe3 ¶
Maybe3 holds three values and an error
Example ¶
package main
import (
"fmt"
"github.com/goaux/results"
)
var _ results.Maybe = results.Maybe3[any, any, any]{}
func main() {
example := func(string) (x, y, z int, err error) { return 8, 7, 6, nil }
x, y, z := results.May3(example("8,7,6")).Or(1, 1, 1)
fmt.Println(x, y, z)
x, y, z = results.May3(example("8,7,6")).OrPanic()
fmt.Println(x, y, z)
example = func(string) (x, y, z int, err error) { return 0, 0, 0, fmt.Errorf("invalid") }
x, y, z = results.May3(example("error")).Or(1, 1, 1)
fmt.Println(x, y, z)
example = func(string) (x, y, z int, err error) { return 0, 0, 0, fmt.Errorf("invalid") }
x, y, z = results.May3(example("error")).OrFunc(
func() (int, int, int) { return 2, 3, 4 },
)
fmt.Println(x, y, z)
}
Output: 8 7 6 8 7 6 1 1 1 2 3 4
func (Maybe3[T0, T1, T2]) Or ¶
func (m Maybe3[T0, T1, T2]) Or(d0 T0, d1 T1, d2 T2) (T0, T1, T2)
Or returns the default values if there's an error, otherwise returns the stored values
func (Maybe3[T0, T1, T2]) OrFunc ¶ added in v1.2.0
func (m Maybe3[T0, T1, T2]) OrFunc(f func() (T0, T1, T2)) (T0, T1, T2)
OrFunc returns the result of the function if there is an error, otherwise it returns the stored value. The function f is called only if there is an error.
type True ¶ added in v1.5.0
type True1 ¶ added in v1.5.0
True1 holds a value and a bool.
Example ¶
package main
import (
"fmt"
"os"
"github.com/goaux/results"
)
func main() {
os.Setenv("EXAMPLE", "example")
os.Unsetenv("EXAMPLE2")
fmt.Println(results.If1(os.LookupEnv("EXAMPLE")).Or("fallback"))
fmt.Println(results.If1(os.LookupEnv("EXAMPLE")).OrPanic("undefined"))
fmt.Println(results.If1(os.LookupEnv("EXAMPLE2")).Or("fallback"))
fmt.Println(results.If1(os.LookupEnv("EXAMPLE2")).OrFunc(func() string { return "fallback" }))
}
Output: example example fallback fallback
func (True1[T]) Or ¶ added in v1.5.0
func (t True1[T]) Or(defaultValue T) T
Or returns t.V0 if t.OK = true, otherwise defaultValue.
func (True1[T]) OrFunc ¶ added in v1.5.0
func (t True1[T]) OrFunc(f func() T) T
OrFunc returns t.V0 if t.OK = true, otherwise the result of calling f.
type True2 ¶ added in v1.5.0
True2 holds values and a bool.
Example ¶
package main
import (
"fmt"
"github.com/goaux/results"
)
func main() {
fmt.Println(results.If2("example", 42, true).Or("fallback", 99))
fmt.Println(results.If2("example", 42, true).OrPanic("undefined"))
fmt.Println(results.If2("example", 42, false).Or("fallback", 99))
fmt.Println(results.If2("example", 42, false).OrFunc(func() (string, int) { return "fallback", 99 }))
}
Output: example 42 example 42 fallback 99 fallback 99
func (True2[T0, T1]) Or ¶ added in v1.5.0
func (t True2[T0, T1]) Or(v0 T0, v1 T1) (T0, T1)
Or returns t.V0 and t.V1 if t.OK = true, otherwise v0, v1.
func (True2[T0, T1]) OrFunc ¶ added in v1.5.0
func (t True2[T0, T1]) OrFunc(f func() (T0, T1)) (T0, T1)
OrFunc returns t.V0 and t.V1 if t.OK = true, otherwise the result of calling f.