1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 21:02:27 +08:00
Files
lancet/docs/xerror.md

1.1 KiB

Xerror

Package xerror implements helpers for errors.

Source:

Usage:

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

Index

Documentation

Unwrap

Unwrap if err is nil then it returns a valid value. If err is not nil, Unwrap panics with err.

Signature:

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

Example:

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
}