4 ms·
It is for deterministically release resources based on scope (although Go's defer is tied to function-scope for reasons I can't understand). python's context ma
by steerablesafe 5y ago
It is for deterministically release resources based on scope (although Go's defer is tied to function-scope for reasons I can't understand). python's context manager is similar. Much like destructors in C++.
You can probably say the same about heap allocated memory, but GC'd languages make the choice the non-deterministic release of heap allocations are fine. But there are resources where you can't play the same game (open files, connections, locks) and you need a way to deterministically close them.
- jatone 5y agodefer is tied to function scope because that is the usecase it was designed for. go also has finalizers executing right before deallocation.