去错误处理技术 [英] Go Error Handling Techniques

查看:116
本文介绍了去错误处理技术的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用Go。我的代码开始有很多这样的:

  if err!= nil {
// handle err

或者这个

  if err:= rows.Scan(& some_column); err!= nil {
//处理错误
}

在Go中检查和处理错误的良好成语/策略/最佳实践?



编辑来澄清:我并不是在暗示或建议Go团队想出一些东西更好。我在问我是否做得对,还是错过了社区提出的一些技巧。感谢所有。 解决方案

你的代码是惯用的,在我看来这是最好的实践。有些人会不同意,但我认为这是标准库在Golang 。换句话说,Go作者以这种方式编写错误处理。


I'm just getting started with Go. My code is starting to have a lot of this:

   if err != nil {
      //handle err
   }

or this

  if err := rows.Scan(&some_column); err != nil {
      //handle err
  }

Are there some good idioms/strategies/best-practices for checking and handling errors in Go?

EDIT to clarify: I'm not bellyaching or suggesting that the Go team come up with something better. I'm asking if I'm doing it right or have I missed some technique that the community came up with. Thanks all.

解决方案

Your code is idiomatic and in my opinion it is the best practice available. Some would disagree for sure, but I would argue that this is the style seen all over the standard libraries in Golang. In other words, Go authors write error handling in this way.

这篇关于去错误处理技术的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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