是否有标准的Err变量? [英] Does go have standard Err variables?

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

问题描述

刚开始使用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.

是否存在standard这样的声明错误类型?例如,在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?

推荐答案

不,没有。只需提供可理解的错误,而不是通用的错误。非法武装运输什么信息?不多,不够。

No, there aren't. Just provide intelligible errors instead of generic ones. What information does a IllegalArgument transport? Not much, not enough.

这篇关于是否有标准的Err变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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