defer keyword is used for declaring a block of code that will wait and will be executed just before exit from current scope (function, closure, loop, etc. ). The block of code inside defer statement is guaranteed to be executed regardless of how we exit from current scope: normally, from a guard, or because of error.
Simple example
As mentioned before, defer statement will be executed before exit from current scope. So, if scope that contains defer statement is inside other scope, than defer will be executed on exit from inner scope.
defer scope example
As we can see here:
Example of usage defer statement when working with file
Source code for this article could be found here on GitHub
Simple example
defer scope
defer scope example
As we can see here:
- Firstly will be printed strings from begin of outer scope
- Secondly will be printed strings from inner scope
- Thirdly will be printed strings from defer that in inner scope (just before exit from inner scope)
- Fourthly will be printed strings from end of outer scope
defer inside for loop
If we will use defer inside, for example, 'for loop', defer statement will be executed before end of each iteration of the loop.
Example of usage defer inside for loop
Multiple defer statements
If there are multiple defer statements inside scope they will be executed in reverse order. Their execution will follow LIFO pattern - Last In First Out.
Multiple defer example
As you can see, order of execution is reversed to order of adding to scope.
defer limitations
There are some limitations for using defer statement. You cannot exit from body of defer statement with following things:
- call return
- call break
- throw an error
Where we can use defer in Real Life Applications
The defer statement can be very useful when we want to clean up before exit from scope (for example, close connections to database or file, release unneeded resources), even if an error is thrown. For example, when we work with file and open a file to write to, we will want to make sure that we close opened file, even if there is an error happened while we work with file.
Example of usage defer statement when working with file
In code snippet above, we close file before exit from function. And we close file in defer statement to make sure that we close file at any case, even if error was being thrown.
Thanks for sharing such useful information. Thanks You I Read Your article its really informative. Keep up the good work. I hope to see more interesting articles from you.
ReplyDelete-Custom Website Design Company
There are many articles circulating on internet that exaggerate about Custom Designed Websites. But your article is an exception that made me understand it without any difficulty.
ReplyDeleteThank you very much for bringing this to my attention. Your blog is chock-full of useful information. I'd have no idea unless I read that. I'll return for more excellent content. Best wishes and best of success to you. Best Custom Websites
ReplyDeleteThanks to your article now I understand or learn many new things which are very difficult to understand the way you describe this topic is very easy to understand. Web Design USA
ReplyDeleteI adore your work, and it greatly motivates me.
ReplyDelete<a href="https://www.iptvfilms.com/things-to-think-about-before-hiring-a-sem-agency/> SEM Agency</a>
I adore your work, and it greatly motivates me.
ReplyDeleteMobile App Testing Services
This is one of the nicest blogs I've ever seen, and it's really
ReplyDeletepleasant. This is a very valuable blog for me, and one of the most
helpful blogs I've ever seen.
Search Engine Marketing Services