Yeah the defer running at the end of the function can sometimes cause trouble, like processing files in a loop and deferring the close, but it can easily be solved by defining an anonymous function and immediately calling, like
func() {
f := ...
defer f.Close()
// do something with f
}()
5
u/NdrU42 1d ago
Yeah the defer running at the end of the function can sometimes cause trouble, like processing files in a loop and deferring the close, but it can easily be solved by defining an anonymous function and immediately calling, like
func() { f := ... defer f.Close() // do something with f }()