error

基础构造

1
2
3
4
// The error built-in interface type is the conventional interface for representing an error condition, with the nil value representing no error.
type error interface {
Error() string
}
1
2
3
4
5
6
7
8
9
10
11
12
13
// New returns an error that formats as the given text. Each call to New returns a distinct error value even if the text is identical.
func New(text string) error {
return &errorString{text}
}

// errorString is a trivial implementation of error.
type errorString struct {
s string
}

func (e *errorString) Error() string {
return e.s
}

规范:errors.New("packageName: error info ")

errors.New()返回的是一个地址,因为如果直接返回结构体,如果两个error的内容相同,可能==会是ture


error
https://www.zengzx.xyz/2023/03/07/01.知识架构/01.Go/01.基础/07.error/
作者
Eden
发布于
2023年3月7日
许可协议