Defer Functions In Golang: Everything You Need To Know

Опубликовано: 20 Май 2026
на канале: Flo Woelki
7,684
324

The defer keyword in Go is a powerful feature you must know! Structs are also really common in Golang and that's why I highly recommend watching this video here as well:    • This is your last video about Golang Structs!  


📝 Description
In this video, we dive deep into one of the most useful and powerful features in Golang—the defer keyword. This concept allows for deferred processing that executes a function just before it returns, making it especially useful for cleanup operations or handling panics. We'll look into three different use-cases: reading a file, logging the execution time of a function, and handling and recovering from panics. Each example will provide you with practical knowledge on how to use the defer keyword effectively in your Golang code.


⏳ Timestamps:
00:00 - Definition of defer
01:37 - 1. Use case - Reading a file
05:55 - 2. Use case - Benchmarking a function
09:39 - 3. Use case - Recover from panics
11:52 - Outro


👋 Hey there!
If you are new to this channel: Hey 👋 my name is Flo and I am a professional software engineer with a passion for coding in Golang, TypeScript, JavaScript, and Rust. This channel is where I share that passion and dive into the exciting world of software engineering. Plus, I love using Emojis and paper-based animations (so have a look for that, if you want to find my videos).


📨 Support and Connect!
Become a member.    / @flowoelki  
Discord:   / discord  
X / Twitter: https://x.com/FlorianWoelki
LinkedIn:   / florian-woelki  
GitHub: https://github.com/FlorianWoelki


📝 Copyright Notice
The original Go Gopher was designed by Renée French (Source: https://go.dev/blog/gopher License details: https://creativecommons.org/licenses/.... The gopher appearing in this video and thumbnail is modified from the original design.


#golang #go #defer