top | item 47086062

(no title)

usrnm | 9 days ago

The go way of dealing with it is wrapping the block with your defers in a lambda. Looks weird at first, but you can get used to it.

discuss

order

mort96|9 days ago

I know. Or in some cases, you can put the loop body in a dedicated function. There are workarounds. It's just bad that the wrong way a) is the most obvious way, and b) is silently wrong in such a way that it appears to work during testing, often becoming a problem only when confronted with real-world data, and often surfacing only as being a hard-to-debug performance or resource usage issue.

9rx|9 days ago

What's the use-case for block-level defer?

In a tight loop you'd want your cleanup to happen after the fact. And in, say, an IO loop, you're going to want concurrency anyway, which necessarily introduces new function scope.