2 ms·
> I wonder what the thought process of the Go designers was when coming up with that approach. Sometimes we need block scoped cleanup, other times we need the
by 0xjnml 8mo ago
> I wonder what the thought process of the Go designers was when coming up with that approach.
Sometimes we need block scoped cleanup, other times we need the function one.
You can turn the function scoped defer into a block scoped defer in a function literal.
AFAICT, you cannot turn a block scoped defer into the function one.
So I think the choice was obvious - go with the more general(izable) variant. Picking the alternative, which can do only half of the job, would be IMO a mistake.
- aw1621107 8mo ago> AFAICT, you cannot turn a block scoped defer into the function one. You kinda-sorta can by creating an array/vector/slice/etc. of thunks (?) in the outer scope and then `defer`ing iterating through/invoking those.