← Back to context

Comment by gf000

5 days ago

Trick question: what is the scope of `defer` in go?

Function body, so no don’t put it in your loop. Just break it out to a helper fn if needed. This isn’t a big problem in practice.

  • Yes, though I think tooling could be better; if I had more spare time I'd write a linter which flagged defers in loops that didn't come with an accompanying comment.

    • I always think, Go is open source why not just fork it to add feature XYZ, then I realize I am better off using languages whose communities appreciate modern language design, instead of wasting my spare time with such things.