This commit is contained in:
github-actions[bot]
2022-09-05 04:28:24 +00:00
parent 7ae51ed0e2
commit f226048bf5
4 changed files with 4 additions and 4 deletions

View File

@@ -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>
<!-- 公众号 -->