result

package module
v0.0.0-...-1060853 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2023 License: MIT Imports: 1 Imported by: 0

README

go-result

Result and Option for golang

Install

go get github.com/ayasechan/go-result

Example

func ExampleResult_downloadFile() {
	download := func(url string, dst io.Writer) Result[struct{}] {
		return WithRecover(func() Result[struct{}] {
			req := AsResult(http.NewRequest(http.MethodGet, url, nil)).Unwrap()
			resp := AsResult(http.DefaultClient.Do(req)).Unwrap()
			defer resp.Body.Close()

			AsResult(io.Copy(dst, resp.Body)).Unwrap()
			return Ok(struct{}{})
		})
	}

	dstFd := AsResult(os.CreateTemp("", ".temp")).Unwrap()
	defer dstFd.Close()
	defer os.Remove(dstFd.Name())

	download("https://httpbin.org/", dstFd).InspectErr(func(err error) {
		fmt.Printf("%+v", err)
		os.Exit(1)
	})
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option[T any] struct {
	// contains filtered or unexported fields
}

func None

func None[T any]() Option[T]

func Some

func Some[T any](value T) Option[T]

func (Option[T]) Cloned

func (opt Option[T]) Cloned() Option[T]

func (Option[T]) Expect

func (opt Option[T]) Expect(msg string) T

func (Option[T]) Filter

func (opt Option[T]) Filter(predicate func(value T) bool) Option[T]

func (Option[T]) IsNone

func (opt Option[T]) IsNone() bool

func (Option[T]) IsSome

func (opt Option[T]) IsSome() bool

func (Option[T]) IsSomeAnd

func (opt Option[T]) IsSomeAnd(f func(value T) bool) bool

func (Option[T]) OkOr

func (opt Option[T]) OkOr(err error) Result[T]

func (Option[T]) OkOrElse

func (opt Option[T]) OkOrElse(err func() error) Result[T]

func (Option[T]) Or

func (opt Option[T]) Or(value Option[T]) Option[T]

func (Option[T]) OrElse

func (opt Option[T]) OrElse(f func() Option[T]) Option[T]

func (Option[T]) Unwrap

func (opt Option[T]) Unwrap() T

func (Option[T]) UnwrapOr

func (opt Option[T]) UnwrapOr(value T) T

func (Option[T]) UnwrapOrDefault

func (opt Option[T]) UnwrapOrDefault() T

func (Option[T]) UnwrapOrElse

func (opt Option[T]) UnwrapOrElse(f func() T) T

type Result

type Result[T any] struct {
	// contains filtered or unexported fields
}
Example (DownloadFile)
Output:

func AsResult

func AsResult[T any](value T, err error) Result[T]

func Err

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

func Ok

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

func WithRecover

func WithRecover[T any](fn func() Result[T]) (ret Result[T])

func (Result[T]) Cloned

func (r Result[T]) Cloned() Result[T]

func (Result[T]) Err

func (r Result[T]) Err() Option[error]

func (Result[T]) Expect

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

func (Result[T]) ExpectErr

func (r Result[T]) ExpectErr(msg string) error

func (Result[T]) Inspect

func (r Result[T]) Inspect(f func(value T)) Result[T]

func (Result[T]) InspectErr

func (r Result[T]) InspectErr(f func(err error)) Result[T]

func (Result[T]) IsErr

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

func (Result[T]) IsErrAnd

func (r Result[T]) IsErrAnd(f func(err error) bool) bool

func (Result[T]) IsOk

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

func (Result[T]) IsOkAnd

func (r Result[T]) IsOkAnd(f func(value T) bool) bool

func (Result[T]) MapErr

func (r Result[T]) MapErr(op func(err error) error) Result[T]

func (Result[T]) Ok

func (r Result[T]) Ok() Option[T]

func (Result[T]) Or

func (r Result[T]) Or(value Result[T]) Result[T]

func (Result[T]) OrElse

func (r Result[T]) OrElse(op func(err error) Result[T]) Result[T]

func (Result[T]) Unwrap

func (r Result[T]) Unwrap() T

func (Result[T]) UnwrapErr

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

func (Result[T]) UnwrapErrUnchecked

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

func (Result[T]) UnwrapOr

func (r Result[T]) UnwrapOr(value T) T

func (Result[T]) UnwrapOrDefault

func (r Result[T]) UnwrapOrDefault() T

func (Result[T]) UnwrapOrElse

func (r Result[T]) UnwrapOrElse(op func(err error) T) T

func (Result[T]) UnwrapUnchecked

func (r Result[T]) UnwrapUnchecked() T

Jump to

Keyboard shortcuts

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