defer inside a loop not running when I expected
I have a loop that opens files and defer f.Close() inside it. I keep running out of file descriptors on a big directory. I thought defer ran at the end of the function, so all the Close calls pile up until the function returns. Is that right? How do people handle cleanup inside loops? Happy to share more detail if it helps.
4 replies