From c05ccc9ec9dd019f8b3f1640626fc64dd5ff450e Mon Sep 17 00:00:00 2001 From: Patrick Cronin Date: Mon, 16 Jul 2018 10:53:31 -0400 Subject: [PATCH] Acknowledge file close error only if one happened The previous version works, but is confusing. In the event that no error results from `f.Close()` and no error resulted during `io.Copy`, it'd set `err` to `nil` (but `err` was already `nil`). The suggested update removes the cause for my confusion and only sets `err` with the error from `f.Close()` in the event that an error actually occurred from that statement, and leaves in place the consideration that no error had occurred from `io.Copy` as well. --- ch5/fetch/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ch5/fetch/main.go b/ch5/fetch/main.go index c83819547..fe2820a2b 100644 --- a/ch5/fetch/main.go +++ b/ch5/fetch/main.go @@ -34,7 +34,7 @@ func fetch(url string) (filename string, n int64, err error) { } n, err = io.Copy(f, resp.Body) // Close file, but prefer error from Copy, if any. - if closeErr := f.Close(); err == nil { + if closeErr := f.Close(); closeErr != nil && err == nil { err = closeErr } return local, n, err