1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00
Files
lancet/docs/xerror_zh-CN.md
2023-02-06 11:42:03 +08:00

1.2 KiB
Raw Blame History

Xerror

xerror 错误处理逻辑封装

源码:

用法:

import (
    "github.com/duke-git/lancet/v2/xerror"
)

目录

文档

Unwrap

检查error, 如果err为nil则展开则它返回一个有效值如果err不是nil则Unwrap使用err发生panic。

函数签名:

func Unwrap[T any](val T, err error) T

示例:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/xerror"
)

func main() {
    result1 := xerror.Unwrap(strconv.Atoi("42"))
    fmt.Println(result1)

    _, err := strconv.Atoi("4o2")
    defer func() {
        v := recover()
        result2 := reflect.DeepEqual(err.Error(), v.(*strconv.NumError).Error())
        fmt.Println(result2)
    }()

    xerror.Unwrap(strconv.Atoi("4o2"))

    // Output:
    // 42
    // true
}