I ended up writing some code that had a lot of possible exit conditions. There's one line of code that always needs to run when it exits, so I put it in a
defer block near the top of the method.
There’s a line of code that needs to run at the end of the method (after everything else has succeeded), but must execute after the deferred statement earlier. So I put it inside a
defer block at the very end of the method, and got a compiler warning:
'defer' statement before end of scope always executes immediately; replace with 'do' statement to silence this warning
I had expected that all deferred blocks (seen so far) would execute in sequence when scope exited. Why is a
defer block at the end of scope treated differently? This seems like an inconsistency from the more desirable behavior I expected.