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 ¶
- type Option
- func (opt Option[T]) Cloned() Option[T]
- func (opt Option[T]) Expect(msg string) T
- func (opt Option[T]) Filter(predicate func(value T) bool) Option[T]
- func (opt Option[T]) IsNone() bool
- func (opt Option[T]) IsSome() bool
- func (opt Option[T]) IsSomeAnd(f func(value T) bool) bool
- func (opt Option[T]) OkOr(err error) Result[T]
- func (opt Option[T]) OkOrElse(err func() error) Result[T]
- func (opt Option[T]) Or(value Option[T]) Option[T]
- func (opt Option[T]) OrElse(f func() Option[T]) Option[T]
- func (opt Option[T]) Unwrap() T
- func (opt Option[T]) UnwrapOr(value T) T
- func (opt Option[T]) UnwrapOrDefault() T
- func (opt Option[T]) UnwrapOrElse(f func() T) T
- type Result
- func (r Result[T]) Cloned() Result[T]
- func (r Result[T]) Err() Option[error]
- func (r Result[T]) Expect(msg string) T
- func (r Result[T]) ExpectErr(msg string) error
- func (r Result[T]) Inspect(f func(value T)) Result[T]
- func (r Result[T]) InspectErr(f func(err error)) Result[T]
- func (r Result[T]) IsErr() bool
- func (r Result[T]) IsErrAnd(f func(err error) bool) bool
- func (r Result[T]) IsOk() bool
- func (r Result[T]) IsOkAnd(f func(value T) bool) bool
- func (r Result[T]) MapErr(op func(err error) error) Result[T]
- func (r Result[T]) Ok() Option[T]
- func (r Result[T]) Or(value Result[T]) Result[T]
- func (r Result[T]) OrElse(op func(err error) Result[T]) Result[T]
- func (r Result[T]) Unwrap() T
- func (r Result[T]) UnwrapErr() error
- func (r Result[T]) UnwrapErrUnchecked() error
- func (r Result[T]) UnwrapOr(value T) T
- func (r Result[T]) UnwrapOrDefault() T
- func (r Result[T]) UnwrapOrElse(op func(err error) T) T
- func (r Result[T]) UnwrapUnchecked() T
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 (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 WithRecover ¶
func (Result[T]) InspectErr ¶
func (Result[T]) UnwrapErrUnchecked ¶
func (Result[T]) UnwrapOrDefault ¶
func (r Result[T]) UnwrapOrDefault() T
func (Result[T]) UnwrapOrElse ¶
func (Result[T]) UnwrapUnchecked ¶
func (r Result[T]) UnwrapUnchecked() T
Click to show internal directories.
Click to hide internal directories.