英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论