登录
首页 >  Golang >  Go问答

Next Error: Implementing the FormatError method

来源:stackoverflow

时间:2024-03-04 13:36:27 341浏览 收藏

目前golang学习网上已经有很多关于Golang的文章了,自己在初次阅读这些文章中,也见识到了很多学习思路;那么本文《Next Error: Implementing the FormatError method》,也希望能帮助到大家,如果阅读完后真的对你学习Golang有帮助,欢迎动动手指,评论留言并分享~

问题内容

https://github.com/golang/xerrors/blob/master/errors.go#l29:47

func (e *errorString) FormatError(p Printer) (next error) {
    p.Print(e.s)
    e.frame.Format(p)
    return nil
}

如果我没记错的话,这总是返回 nil 对吗?如果 next 始终是 nil ,那么它的目的是什么?


解决方案


那么下一步的目的是什么?

formaterror(p printer)(下一个错误) 方法满足接口。

// a formatter formats error messages.
type formatter interface {
    error

    // formaterror prints the receiver's first error and returns the next error in
    // the error chain, if any.
    formaterror(p printer) (next error)
}

有时我们确实会返回非零错误。

func (e *noWrapError) FormatError(p Printer) (next error) {
    p.Print(e.msg)
    e.frame.Format(p)
    return e.err
}

好了,本文到此结束,带大家了解了《Next Error: Implementing the FormatError method》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>