Go 有标准的 Err 变量吗? [英] Does Go have standard Err variables?
问题描述
刚开始使用 Golang.我认为声明一个错误变量并在错误结构中使用它来确定出了什么问题是惯用的,就像在 strconv.go.在那里,声明了 ErrRange
和 ErrSyntax
,并且在适当的时候,对它们的引用在返回时存储在 NumError
结构中.我认为原因是因为这样可以将存储在 NumError
中的错误引用的地址与 ErrRange
和 ErrSyntax
变量进行比较确定返回的错误类型.
Just started using Golang. I think that it is idiomatic to declare an error variable and use it in your error structure to determine what went wrong, as is done in strconv.go. There, ErrRange
and ErrSyntax
is declared, and when appropriate, references to those are stored in NumError
structs when they return. I think that the reason is because then the address of the reference to the error stored in NumError
can be compared with the ErrRange
and ErrSyntax
variables to determine which type of error was returned.
是否有标准"这样声明的错误类型?例如,在 Java 中,您有诸如 java.lang.IllegalArgumentException
之类的东西.例如,是否有 ErrArgument
或 ErrUnsupportedOperation
可以在我自己的代码中使用,而不是创建每次都表示相同内容的新错误变量?
Are there "standard" such declared error types? In Java, for example, you have things like java.lang.IllegalArgumentException
. Is there, for instance, ErrArgument
or ErrUnsupportedOperation
that I can use in my own code instead of creating new error variables that mean the same thing every time?
推荐答案
不,没有.只需提供可理解的错误而不是通用错误.IllegalArgument 传输什么信息?不多,还不够.
No, there aren't. Just provide intelligible errors instead of generic ones. What information does a IllegalArgument transport? Not much, not enough.
这篇关于Go 有标准的 Err 变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!