distr1 / distri

a Linux distribution to research fast package management
https://distr1.org
Other
536 stars 26 forks source link

internal/cp/copy.go: defer file.Close() confusion #44

Closed dalechyn closed 5 years ago

dalechyn commented 5 years ago

Hey. I am a little confused in what is going on with deferring files closing in internal/cp/copy.go.

if err := out.Close(); err != nil {
    return err
}

Isn't it an extra call to out.Close() since we have defer out.Close()?

If it is not, and It's just me missing something with Go defer call handling, why there is no error check for defer in.Close()?

stapelberg commented 5 years ago

The deferred call is for avoiding resource leaks (we don’t care if it fails), the non-deferred call is for checking whether flushing the file to disk fails.

This is intentional :)