douao3063 2018-08-24 20:12
浏览 651

检查对fmt.Errorf()的调用是否失败[关闭]

In Go, the function fmt.Errorf() returns an error interface.
What's the proper way to check if fmt.Errorf() itself failed?

  • 写回答

3条回答 默认 最新

  • 普通网友 2018-08-24 20:18
    关注

    Errorf formats according to a format specifier and returns the string as a value that satisfies error. The fmt package's Errorf function lets us use the package's formatting features to create descriptive error messages.

    func Errorf(format string, a ...interface{}) error {
        return errors.New(Sprintf(format, a...))
    }
    

    And New returns an error that formats as the given text.

    func New(text string) error {
        return &errorString{text}
    }
    

    And errorString is a trivial implementation of error.

    type errorString struct {
        s string
    }
    

    So Basically what it is returning is field of string type.

    评论

报告相同问题?