result

package
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 28, 2024 License: MIT Imports: 2 Imported by: 0

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] 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 Err

func Err[T any](err error) Result[T]

Err instantiates a Result with an error.

func Ok

func Ok[T any](value T) Result[T]

Ok instantiates a Result with a value.

func (Result[T]) Expect added in v0.19.0

func (r Result[T]) Expect(message string) T

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

func (r Result[T]) IsErr() bool

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

func (r Result[T]) IsOk() bool

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

func (r Result[T]) String() string

String implements the fmt.Stringer interface.

func (*Result[T]) UnmarshalJSON added in v0.17.0

func (r *Result[T]) UnmarshalJSON(data []byte) error

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

func (r Result[T]) UnwrapErr() error

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
}
Output:

func (Result[T]) Value

func (r Result[T]) Value() (T, error)

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>

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL