I have two examples about defer statement in golang, the first one is incorrect, and the second one is correct. But I think they have the same problem, in my opinion I think the second still has risk that run out of file descriptors, could any one can help me to clarify why the second is correct? Thanks!
Example1:
for _, filename := range filenames {
f, err := os.Open(filename)
if err != nil {
return err
}
defer f.Close() // NOTE: risky; could run out of file descriptors
// ...process f...
}
Example2:
for _, filename := range filenames {
if err := doFile(filename); err != nil {
return err
}
}
func doFile(filename string) error {
f, err := os.Open(filename)
if err != nil {
return err
}
defer f.Close()
// ...process f...
}
Deferred functions run when the enclosing function returns. Spec: Defer statements:
A "defer" statement invokes a function whose execution is deferred to the moment the surrounding function returns, either because the surrounding function executed a return statement, reached the end of its function body, or because the corresponding goroutine is panicking.
In your first example you use a single for
loop in which you use defer
statements, but the deferred functions will not get executed at the end of the iterations, only after the for
loop (when the enclosing function ends). This means all the files you open inside the for
loop will remain open 'till the "end". If filenames
contains a thousand valid file names, the loop will (try to) open a thousand files before starting to close any of them.
In your second example you "outsourced" dealing with the files to a separate function. Using defer
in this function is fundamentally different, as when this function returns, the deferred function (File.Close()
) will be called first. So in your second example at most 1 file is open at all times by the posted code, no matter how many elements filenames
contains.