4 ms·
Demystifying Go's 'Defer' Statement
- bitfield 4y agoWhat problem does 'defer' solve? When should we use it? How does it interact with named results? What's the use of deferring a closure? Is it okay to write naked returns?
- dovholuknf 4y agoone common use is the 'try/catch' mechanic. if a panic happens in go, and you want to be able to recover you'd use a defer. it's also used in the 'autoclosable' type way. It's easy when opening a resource, to 'defer closing' them right then / there. go takes a slow, methodical approach to feature changes. sometimes people think 'too slow', some think it's fine. there are times when you 'defer' and you think - mmm that was nice. sometimes it's "ugh i guess i have to defer this now...." it has its uses