Go 有标准的 Err 变量吗? [英] Does Go have standard Err variables?

查看:21
本文介绍了Go 有标准的 Err 变量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚开始使用 Golang.我认为声明一个错误变量并在错误结构中使用它来确定出了什么问题是惯用的,就像在 strconv.go.在那里,声明了 ErrRangeErrSyntax,并且在适当的时候,对它们的引用在返回时存储在 NumError 结构中.我认为原因是因为这样可以将存储在 NumError 中的错误引用的地址与 ErrRangeErrSyntax 变量进行比较确定返回的错误类型.

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 之类的东西.例如,是否有 ErrArgumentErrUnsupportedOperation 可以在我自己的代码中使用,而不是创建每次都表示相同内容的新错误变量?

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆