Skip to content

Commit

Permalink
update directory
Browse files Browse the repository at this point in the history
  • Loading branch information
songleo committed Mar 23, 2016
1 parent 1536c36 commit e085ad4
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
2 changes: 1 addition & 1 deletion eBook/16.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ if something {
}
```

此类型的错误也容易在`for`循环中出现,尤其当函数返回一个具名变量时难于察觉
此类错误也容易在`for`循环中出现,尤其当函数返回一个具名变量时难于察觉
,例如以下的代码段:

```go
Expand Down
6 changes: 3 additions & 3 deletions eBook/16.3.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 16.3 发生错误时使用defer关闭一个文件

如果你在一个for循环内部处理一些列文件,你需要使用defer确保文件在处理完毕后被关闭,例如:
如果你在一个for循环内部处理一系列文件,你需要使用defer确保文件在处理完毕后被关闭,例如:

```go
for _, file := range files {
Expand All @@ -14,7 +14,7 @@ for _, file := range files {
}
```

但是在循环结尾处的defer没有执行,所以文件一直没有关闭!垃圾回收机制可能会自动关闭文件,但是这回产生一个错误,更好的做法应该是
但是在循环结尾处的defer没有执行,所以文件一直没有关闭!垃圾回收机制可能会自动关闭文件,但是这会产生一个错误,更好的做法是

```go
for _, file := range files {
Expand All @@ -28,7 +28,7 @@ for _, file := range files {
}
```

**defer仅在函数返回时才会执行,在循环的结尾或者其他一些有限的范围不会执行**
**defer仅在函数返回时才会执行,在循环的结尾或其他一些有限范围的代码内不会执行**

## 链接

Expand Down
3 changes: 3 additions & 0 deletions eBook/directory.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@

- 第16章:[常见的陷阱与错误](16.0.md)
- 16.1 [误用短声明导致变量覆盖](16.1.md)
- 16.2 [误用字符串](16.2.md)
- 16.3 [发生错误时使用defer关闭一个文件](16.3.md)
- 16.4 [何时使用new()和make()](16.4.md)
- 第17章:模式
- 第18章:[出于性能考虑的实用代码片段](18.0.md)
- 18.1 [字符串](18.1.md)
Expand Down

0 comments on commit e085ad4

Please sign in to comment.