Skip to content

Commit 3ed31b5

Browse files
committed
update files 2019年 5月17日 星期五 17时59分41秒 CST
1 parent fcd8845 commit 3ed31b5

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

errors.md

+10
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ that it's best to capture and check for errors in all database operations. If
6464
Logging the error message or panicing might be the only sensible thing,
6565
and if that's not sensible, then perhaps you should just ignore the error.
6666

67+
`rows.Close()` 返回的错误是一般规则的唯一例外,它最好捕获并检查所有数据库操作中的错误。如果rows.Close()返回错误,则不清楚应该怎么做。记录错误消息或panicing可能是唯一明智的事情,如果这不合理,那么也许你应该忽略错误。
68+
6769
Errors From QueryRow()
6870
======================
6971

@@ -81,15 +83,21 @@ fmt.Println(name)
8183
What if there was no user with `id = 1`? Then there would be no row in the
8284
result, and `.Scan()` would not scan a value into `name`. What happens then?
8385

86+
如果没有id = 1的用户怎么办?然后结果中没有行,并且.Scan()不会将值扫描到名称中。那么会发生什么?
87+
8488
Go defines a special error constant, called `sql.ErrNoRows`, which is returned
8589
from `QueryRow()` when the result is empty. This needs to be handled as a
8690
special case in most circumstances. An empty result is often not considered an
8791
error by application code, and if you don't check whether an error is this
8892
special constant, you'll cause application-code errors you didn't expect.
8993

94+
Go定义了一个特殊的错误常量,称为sql.ErrNoRows,当结果为空时,它从QueryRow()返回。在大多数情况下,这需要作为特殊情况处理。应用程序代码通常不会将空结果视为错误,如果不检查错误是否为此特殊常量,则会导致您没有预料到的应用程序代码错误。
95+
9096
Errors from the query are deferred until `Scan()` is called, and then are
9197
returned from that. The above code is better written like this instead:
9298

99+
查询中的错误将被推迟,直到调用 `Scan()` ,然后从中返回。上面的代码更好地编写如下:
100+
93101
<pre class="prettyprint lang-go">
94102
var name string
95103
err = db.QueryRow("select name from users where id = ?", 1).Scan(&amp;name)
@@ -110,6 +118,8 @@ to use this special-case in order to let the caller distinguish whether
110118
you might not realize that your variable didn't get any value from the database
111119
after all.
112120

121+
有人可能会问为什么空结果集被认为是错误。空集没有任何错误。原因是QueryRow()方法需要使用这种特殊情况才能让调用者区分QueryRow()实际上是否找到了一行;没有它,Scan()不会做任何事情,你可能没有意识到你的变量毕竟没有从数据库中获得任何值。
122+
113123
You should only run into this error when you're using `QueryRow()`. If you
114124
encounter this error elsewhere, you're doing something wrong.
115125

0 commit comments

Comments
 (0)