8000 GitHub - chyroc/anyhow: error handle
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

chyroc/anyhow

Repository files navigation

anyhow

go impl for https://docs.rs/anyhow/latest/anyhow/

codecov go report card test status Apache-2.0 license Go.Dev reference Go project version

Installation

go get github.com/chyroc/anyhow

Features

  • result struct
    • Result1[T]
    • Result2[T1, T2]
    • Result3[T1, T2, T3]
    • Result4[T1, T2, T3, T4]
    • Result5[T1, T2, T3, T4, T5]
    • Result6[T1, T2, T3, T4, T5, T6]
  • functions:
    • And(self R[T], res R[U]) R[U]: Returns res if the result is Ok, otherwise returns the Err value of self
    • AndThen(self R[T], op (T) -> R[U]) R[U]: Calls op if the result is Ok, otherwise returns the Err value of self
    • Map(self R[T], op (T) -> U) R[U]: Maps a Result to Result by applying a function to a contained Ok value, leaving an Err value untouched
    • MapOr(self R[T], default U, op func(T) U) U: Returns the provided default (if Err), or applies a function to the contained value (if Ok)
  • methods:
    • (R[T]) IntoErr() error: Returns the contained Err value, but never panics
    • (R[T]) IntoOk() T1: Returns the contained Ok value, but never panics
    • (R[T]) Expect(string) T: Returns the contained Ok value, otherwise panics with a panic message including the passed message, and the content of the Err
    • (R[T]) ExpectErr(string) error: Returns the contained Err value, otherwise panics with a panic message including the passed message, and the content of the Ok
    • (R[T]) Inspect(f (T) -> void) R[T]: Calls a function with a reference to the contained value if Ok
    • (R[T]) InspectErr(f (error) -> void) R[T]: Calls a function with a reference to the contained value if Err
    • (R[T]) MapErr(op func(error) error) R[T]: Maps a R[T] to R[T] by applying a function to a contained Err value, leaving an Ok value untouched
    • (R[T]) IsErr() bool: Returns true if the result is Err
    • (R[T]) IsOk() bool: Returns true if the result is Ok
    • (R[T]) Or(res R[T]) R[T]: Returns res if the result is Err, otherwise returns the Ok value of self
    • (R[T]) OrElse(op (error) -> R[T]) R[T]: Calls op if the result is Err, otherwise returns the Ok value of self
    • (R[T]) Unwrap() T: Returns the contained Ok value, otherwise panics with Err
    • (R[T]) UnwrapOr(default T) T: Returns the contained Ok value or a provided default
    • (R[T]) UnwrapOrDefault() T: Returns the contained Ok value or a default value
    • (R[T]) UnwrapOrElse(op (error) -> T) T: Returns the contained Ok value or computes it from a closure

Usage

package main

import (
  "log"
  "os/user"

  . "github.com/chyroc/anyhow"
)

func getHomePath() Result1[string] {
  user, err := user.Current()
  if err != nil {
    return Err1[string](err)
  }
  return Ok1(user.HomeDir)
}

func Example_AndThen_UnwrapOr() {
  // get home path, and then get log path, or use /tmp/log
  _ = AndThen11(getHomePath(), func(t1 string) Result1[string] {
    return Ok1(t1 + "/.log")
  }).UnwrapOr("/tmp/log")
}

func Example_Inspect_InspectErr() {
  // get home path, log it, or log error
  _ = getHomePath().Inspect(func(t1 string) {
    log.Println("home:", t1)
  }).InspectErr(func(err error) {
    log.Println("error:", err)
  }).UnwrapOr("/tmp/log")
}

func Example_Expect_ExpectErr() {
  // get home path, or panic with `must get home` msg
  _ = getHomePath().Expect("must get home")

  // get home path with err, or panic with `must get err` msg
  _ = getHomePath().ExpectErr("must get err")
}

About

error handle

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

0