zlacker

[parent] [thread] 3 comments
1. grey-a+(OP)[view] [source] 2025-08-22 11:37:45
There’s probably no deep reason, does it matter much?
replies(2): >>torgin+D7 >>wtetzn+fy
2. torgin+D7[view] [source] 2025-08-22 12:30:30
>>grey-a+(OP)
Yes it does, function-scope defer needs a dynamic data structure to keep track of pending defers, so its not zero cost.

It can be also a source of bugs where you hang onto something for longer than intended - considering there's no indication of something that might block in Go, you can acquire a mutex, defer the release, and be surprised when some function call ends up blocking, and your whole program hangs for a second.

replies(1): >>nasret+7B
3. wtetzn+fy[view] [source] 2025-08-22 14:54:28
>>grey-a+(OP)
Having to wrap a loop body in a function that's immediately invoked seems like it would make the code harder to read. Especially for a language that prides itself on being "simple" and "straightforward".
◧◩
4. nasret+7B[view] [source] [discussion] 2025-08-22 15:09:20
>>torgin+D7
I think it's only a real issue when you're coming from a language that has different rules. Block-scoping (and thus not being able to e.g. conditionally remove a temp file at the end of a function) would be equally surprising for someone coming from Go.

But I do definitely agree that the dynamic nature of defer and it not being block-scoped is probably not the best

[go to top]