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

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

问题描述

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

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

   if err != nil {
      //handle err
   }

或者这个

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

是否有一些好的习惯用法/策略/最佳实践来检查和处理 Go 中的错误?

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.

推荐答案

您的代码是惯用的,在我看来这是可用的最佳实践.有些人肯定不同意,但我认为这是在 标准库中随处可见的风格在 Golang 中.换句话说,Go 作者就是用这种方式编写错误处理的.

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.

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

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