3 ms·
Is Go’s defer used to optimize the GC? I thought it was just syntax sugar to make sure people wouldn’t forget to close file handles and things like that..?
by josephg 5y ago
Is Go’s defer used to optimize the GC? I thought it was just syntax sugar to make sure people wouldn’t forget to close file handles and things like that..?
- steerablesafe 5y agoIt 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.