-

@ Machu Pikacchu
2025-05-27 19:29:33
golang could benefit from something like “defer” but for for-loop bodies.
If you open files in a loop you don’t want to defer closing them because then you have a lot of open file handles, but if you don’t use defer then you have to explicitly close them anywhere you have an error check or “continue” in the loop body.
Sure you can solve that by creating a new function for that loop body and do the defer in there but sometimes that can lead to other code smells.
It would also be convenient for zeroing out slices in each loop.