【问题标题】:Error type go lang错误类型 golang
【发布时间】:2017-02-22 15:45:01
【问题描述】:

Go 中的错误类型是“错误”还是“错误”?它困扰着我,在Tour 中,它的首字母很小,所以我环顾四周,发现here with small ehere in source code 它的首字母很大。 另外,如果没有大写字母,但在包装外仍然可见,怎么可能?

刚开始学习 Go,所以我可能错过了一些基本的东西,谢谢。

【问题讨论】:

  • 所以我试过了,即使导入了runtime 包,我也不能输入Error,它说imported and not used: "runtime"
  • 它是 error 全小写,因为它是预先声明的类型,如下所述:golang.org/ref/spec#Errors
  • runtime.Errorerror 的类型不同。

标签: go


【解决方案1】:

error 是类型,小写。就像 intstring 一样,它不需要可见,因为它是 Go 内置的:

A good blog post on error handling

您所指的runtime 包有一个Error 接口。类型有 interface 而不是 error:

Package runtime

type Error interface {
    error

    // RuntimeError is a no-op function but
    // serves to distinguish types that are run time
    // errors from ordinary errors: a type is a
    // run time error if it has a RuntimeError method.
    RuntimeError()
}

Error 接口识别运行时错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-28
    • 1970-01-01
    • 2016-10-03
    • 2017-09-21
    • 2021-11-17
    • 2014-01-22
    • 2018-12-06
    • 2016-02-27
    相关资源
    最近更新 更多