db.ExecContext returns an error and the result which also returns an error. Does it make sense to check error twice for the same operation?

huangapple go评论84阅读模式
英文:

db.ExecContext returns an error and the result which also returns an error. Does it make sense to check error twice for the same operation?

问题

这是一段代码示例:

r, err := s.GetDB().ExecContext(ctx, query)
if err != nil {
  return err
}
count, err := r.RowsAffected()
if err != nil { // 这里是不是多余的检查?
   return err
}

我的观点是,如果在执行 Exec 后出现错误,那么会进行报告,为什么还需要再次检查呢?

英文:

this is a sample of code:

r, err := s.GetDB().ExecContext(ctx, query)
if err != nil {
  return err
}
count, err := r.RowsAffected()
if err != nil { // is not it double check?
   return err
}

My opinion is that if there was an error it will be reported after Exec, why one need to check it for the second time?

答案1

得分: 3

也许文档中的这一行解释了为什么在使用RowsAffected()之后需要检查错误:
并非每个数据库或数据库驱动程序都支持此功能

英文:

Perhaps this line from documentation explains why one needs to check an error after RowsAffected():
Not every database or database driver may support this

huangapple
  • 本文由 发表于 2022年10月12日 20:27:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/74041804.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定