▲ | grey-area 4 days ago | |||||||
There’s probably no deep reason, does it matter much? | ||||||||
▲ | torginus 4 days ago | parent | next [-] | |||||||
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. | ||||||||
| ||||||||
▲ | wtetzner 4 days ago | parent | prev [-] | |||||||
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". |