Defer - Go Lang Practical Programming Tutorial p.20
Key Takeaways
The video discusses the defer statement in Go programming language, its use in handling errors and ensuring code execution, and introduces the concept of panic and recover.
Full Transcript
what's going on everybody welcome to part 20 of the go language programming tutorial series in this tutorial what we're going to be talking about is the defer statement in ingo so what we're talking about in the last tutorial was hey we're using this kind of weight group and all that and it's really cool but what if we run into something that causes this function to error and we we don't actually get to tell the weight group hey we're done well then we're gonna have a problem we're gonna be waiting all right and we don't want that so this isn't a problem that you're gonna come across with just doing go routines and weight groups for example when you want to close connections when you're done or close files or whatever if you hit an error before you get to the clothes you're never gonna actually run that operation so in go they have the defer statement so the idea of defer is it'll be evaluated whenever it gets hit but actually the defer statement won't run whatever's being deferred until the surrounding function either completes or panics out so if the if once it's been evaluated if that function does happen to panic out all of the deferred functions will run so if it's an error or if it doesn't hit an error basically whenever that function is all set and done it's gonna go ahead and run whatever you decided to defer so to exemplify this I'm gonna go ahead and clear clean up here we'll come back to this you can I would recommend you save this because I'm just going to copy and paste it over when we return back to this but I just want to use a more simple example for now so let's say we've got a func we'll call it foo and foo basically just does a format dot print line and you know this is a function that does some stuff who knows right and then funk mein can run Fuu okay so if we wanted to we could we could say okay while this function is doing some stuff we want to make sure when it's done doing some stuff we want to make sure it runs format dot print line done so this is gonna be evaluated immediately but it's only gonna run when the rest of the foo function is done so let me pull this up and let's run that real quick and we can see yeah it says doing some stuff who knows what and then it's done running now what if we added another like what if we threw in another defer statement so we should so let's do [Music] are we done so the way to first statements work is in a first in last out order I'm not really sure why they why it's that way but basically that also means last in will be first out so what we're gonna see when we run this is doing some stuff are we done and then done so for just to show that right sure enough that's that's how it ran so we could also exemplify that with like a for loop or something like that so for example we could we could say for I colon equals 0 Wow I less than 5 we could just say defer format print line high so save that run it and sure enough it basically counts down right so getting back to our main example I'm just gonna kind of copy and paste it over copy paste so this is our original example where we kind of came into this so this one it works and it waits so just to show right it just runs those two go routines waits for them to finish but if wait group done never runs we're in trouble so what we could say is rather than running WG gun down here we'll just cut that and then we defer wait group done to the top so then we can save that and and now we can rerun function or the program and now we've got hey there hey there hey there okay so that is how we can defer things and why we might want to defer things and if we're going to be talking about the defer statement we might as well also talk about panic and recover in the go programming language so while we can use defer to make sure something happens we can also use defer to recover after a panic has occurred so so you could defer some sort of function that's gonna handle for whatever the error was that we might have actually hid that way your program doesn't stop running or at least it doesn't have to you could let it stop running but you don't have to do that anyway in the next tutorial that's we're gonna be talking about we should have no problem covering both panic and recover in the next tutorials so panic is a way that we can say hey we've hit an error based on some sort of condition so we can panic out but also like I was saying defer doesn't necessarily only run if the function successful defer will also all the deferred statements are gonna run if the function also panics out so if the surrounding function panics out all the deferred functions will also run so then within within those deferred functions we can recover from panics so anyways that's what we're going to talk about in the next tutorial if you have questions comments concerns whatever feel free to Livan below otherwise I'll see you in the next tutorial
Original Description
Welcome to part 20 of the Go programming tutorial, where we will be introducing the defer statement and its use in Go. The idea of the defer statement is to put off (defer) the execution of something until the surrounding function is done.
Text tutorials and sample code: https://pythonprogramming.net/go/
https://twitter.com/sentdex
https://www.facebook.com/pythonprogramming.net/
https://plus.google.com/+sentdex
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from sentdex · sentdex · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
Python Encryption Tutorial with PyCrypto
sentdex
Python's Logging Function
sentdex
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
wxPython Programming Tutorial 4: Panels
sentdex
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
wxPython Programming Tutorial 8: Custom Button Images
sentdex
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
Basic PHP Tutorial 12: Associative Array
sentdex
Basic PHP Tutorial 14: Foreach loop
sentdex
Basic PHP Tutorial 16: Include and Require
sentdex
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
Basic PHP Tutorial 4: Variables and Comments
sentdex
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
Basic PHP Tutorial 1: Intro to PHP
sentdex
Basic PHP Tutorial 3: HTML with PHP
sentdex
Basic PHP Tutorial 9: While Loop
sentdex
Basic PHP Tutorial 10: Switch Statement
sentdex
Basic PHP Tutorial 2: Print and Echo
sentdex
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
Basic PHP Tutorial 19: Finding words in strings
sentdex
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
Basic PHP Programming Tutorial 24: String similarity
sentdex
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
Sikuli Tutorial 1: Visually programming in python!
sentdex
Sikuli Tutorial 2: Program visually in python!
sentdex
Sikuli Tutorial 3: Program visually in python!
sentdex
3D Bar Charts in Python and Matplotlib
sentdex
3D Plane wire frame Graph Chart in Python
sentdex
Raspberry Pi Part 1 Introduction
sentdex
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
Raspberry Pi Part 11: Remote Desktop
sentdex
Twitter Analysis: How to rank a user's influence
sentdex
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex
Related Reads
📰
📰
📰
📰
How I Built a Free Online Image & PDF Processing Platform with Vue 3 + FastAPI
Dev.to · IAMUU
I Built a Free AI-Powered YouTube SEO Toolkit With Zero Budget. Here’s What Actually Happened.
Medium · Startup
How to Create a Second Version of Yourself Inside Obsidian Using AI (Step-by-Step Guide)
Medium · ChatGPT
How to prepare for Spain civil service TIC exam using AI in 2026
Dev.to · David García
🎓
Tutor Explanation
DeepCamp AI