mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2025-12-17 11:14:20 +08:00
deploy: fc4e5fdd3e
This commit is contained in:
@@ -338,7 +338,7 @@ func fetch(url string) (filename string, n int64, err error) {
|
||||
}
|
||||
</code></pre>
|
||||
<p>对resp.Body.Close延迟调用我们已经见过了,在此不做解释。上例中,通过os.Create打开文件进行写入,在关闭文件时,我们没有对f.close采用defer机制,因为这会产生一些微妙的错误。许多文件系统,尤其是NFS,写入文件时发生的错误会被延迟到文件关闭时反馈。如果没有检查文件关闭时的反馈信息,可能会导致数据丢失,而我们还误以为写入操作成功。如果io.Copy和f.close都失败了,我们倾向于将io.Copy的错误信息反馈给调用者,因为它先于f.close发生,更有可能接近问题的本质。</p>
|
||||
<p>**练习5.18:**不修改fetch的行为,重写fetch函数,要求使用defer机制关闭文件。</p>
|
||||
<p><strong>练习5.18:</strong> 不修改fetch的行为,重写fetch函数,要求使用defer机制关闭文件。</p>
|
||||
|
||||
|
||||
<!-- 公众号 -->
|
||||
|
||||
Reference in New Issue
Block a user