Golang Defer: Heap-allocated, Stack-allocated, Open-coded Defer
This is an excerpt of the post; the full post is available here: Golang Defer: From Basic To Trap.
The defer statement is probably one of the first things we find pretty interesting when we start learning Go, right?
But there's a lot more to it that trips up many people, and there're many fascinating aspects that we often don't touch on when using it.
For example, the defer statement actually has 3 types (as of Go 1.22, though that might change later): open-coded defer, heap-allocated defer, and stack-allocated. Each one has different performance and different scenarios where they're best used, which is good to know if you want to optimize performance.
In this discussion, we're going to cover everything from the basics to the more advanced usage, and we'll even dig a bit, just a little bit, into some of the internal details.
What is defer?
Let's take a quick look at defer before we dive too deep.
In Go, defer is a keyword used to delay the execution of a function until the surrounding function finishes.
func main() { defer fmt.Println("hello") fmt.Println("world") } // Output: // world // hello
In this snippet, the defer statement schedules fmt.Println("hello") to be executed at the very end of the main function. So, fmt.Println("world") is called immediately, and "world" is printed first. After that, because we used defer, "hello" is printed as the last step before main finishes.
It's just like setting up a task to run later, right before the function exits. This is really useful for cleanup actions, like closing a database connection, freeing up a mutex, or closing a file:
func doSomething() error { f, err := os.Open("phuong-secrets.txt") if err != nil { return err } defer f.Close() // ... }
The code above is a good example to show how defer works, but it's also a bad way to use defer. We'll get into that in the next section.
"Okay, good, but why not put the f.Close() at the end?"
There are a couple of good reasons for this:
- We put the close action near the open, so it's easier to follow the logic and avoid forgetting to close the file. I don't want to scroll down a function to check if the file is closed or not; it distracts me from the main logic.
- The deferred function is called when the function returns, even if a panic (runtime error) happens.
When a panic happens, the stack is unwound and the deferred functions are executed in a specific order, which we'll cover in the next section.
Defers are stacked
When you use multiple defer statements in a function, they are executed in a 'stack' order, meaning the last deferred function is executed first.
func main() { defer fmt.Println(1) defer fmt.Println(2) defer fmt.Println(3) } // Output: // 3 // 2 // 1
Every time you call a defer statement, you're adding that function to the top of the current goroutine's linked list, like this:
And when the function returns, it goes through the linked list and executes each one in the order shown in the image above.
But remember, it does not execute all the defer in the linked list of goroutine, it's only run the defer in the returned function, because our defer linked list could contain many defers from many different functions.
func B() { defer fmt.Println(1) defer fmt.Println(2) A() } func A() { defer fmt.Println(3) defer fmt.Println(4) }
So, only the deferred functions in the current function (or current stack frame) are executed.
But there's one typical case where all the deferred functions in the current goroutine get traced and executed, and that's when a panic happens.
Defer, Panic and Recover
Besides compile-time errors, we have a bunch of runtime errors: divide by zero (integer only), out of bounds, dereferencing a nil pointer, and so on. These errors cause the application to panic.
Panic is a way to stop the execution of the current goroutine, unwind the stack, and execute the deferred functions in the current goroutine, causing our application to crash.
To handle unexpected errors and prevent the application from crashing, you can use the recover function within a deferred function to regain control of a panicking goroutine.
func main() { defer func() { if r := recover(); r != nil { fmt.Println("Recovered:", r) } }() panic("This is a panic") } // Output: // Recovered: This is a panic
Usually, people put an error in the panic and catch that with recover(..), but it could be anything: a string, an int, etc.
In the example above, inside the deferred function is the only place you can use recover. Let me explain this a bit more.
There are a couple of mistakes we could list here. I’ve seen at least three snippets like this in real code.
The first one is, using recover directly as a deferred function:
func main() { defer recover() panic("This is a panic") }
The code above still panics, and this is by design of the Go runtime.
The recover function is meant to catch a panic, but it has to be called within a deferred function to work properly.
Behind the scenes, our call to recover is actually the runtime.gorecover, and it checks that the recover call is happening in the right context, specifically from the correct deferred function that was active when the panic occurred.
"Does that mean we can’t use recover in a function inside a deferred function, like this?"
func myRecover() { if r := recover(); r != nil { fmt.Println("Recovered:", r) } } func main() { defer func() { myRecover() // ... }() panic("This is a panic") }
Exactly, the code above won’t work as you might expect. That’s because recover isn’t called directly from a deferred function but from a nested function.
Now, another mistake is trying to catch a panic from a different goroutine:
func main() { defer func() { if r := recover(); r != nil { fmt.Println("Recovered:", r) } }() go panic("This is a panic") time.Sleep(1 * time.Second) // Wait for the goroutine to finish }
Makes sense, right? We already know that defer chains belong to a specific goroutine. It would be tough if one goroutine could intervene in another to handle the panic since each goroutine has its own stack.
Unfortunately, the only way out in this case is crashing the application if we don’t handle the panic in that goroutine.
Defer arguments, including receiver are immediately evaluated
I've run into this problem before, where old data got pushed to the analytics system, and it was tough to figure out why.
Here’s what I mean:
func pushAnalytic(a int) { fmt.Println(a) } func main() { a := 10 defer pushAnalytic(a) a = 20 }
What do you think the output will be? It's 10, not 20.
That's because when you use the defer statement, it grabs the values right then. This is called "capture by value." So, the value of a that gets sent to pushAnalytic is set to 10 when the defer is scheduled, even though a changes later.
There are two ways to fix this.
...
Full post is available here: Golang Defer: From Basic To Trap.
The above is the detailed content of Golang Defer: Heap-allocated, Stack-allocated, Open-coded Defer. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Go language performs well in building efficient and scalable systems. Its advantages include: 1. High performance: compiled into machine code, fast running speed; 2. Concurrent programming: simplify multitasking through goroutines and channels; 3. Simplicity: concise syntax, reducing learning and maintenance costs; 4. Cross-platform: supports cross-platform compilation, easy deployment.

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

Golang is better than C in concurrency, while C is better than Golang in raw speed. 1) Golang achieves efficient concurrency through goroutine and channel, which is suitable for handling a large number of concurrent tasks. 2)C Through compiler optimization and standard library, it provides high performance close to hardware, suitable for applications that require extreme optimization.

Goimpactsdevelopmentpositivelythroughspeed,efficiency,andsimplicity.1)Speed:Gocompilesquicklyandrunsefficiently,idealforlargeprojects.2)Efficiency:Itscomprehensivestandardlibraryreducesexternaldependencies,enhancingdevelopmentefficiency.3)Simplicity:

Golang and Python each have their own advantages: Golang is suitable for high performance and concurrent programming, while Python is suitable for data science and web development. Golang is known for its concurrency model and efficient performance, while Python is known for its concise syntax and rich library ecosystem.

The performance differences between Golang and C are mainly reflected in memory management, compilation optimization and runtime efficiency. 1) Golang's garbage collection mechanism is convenient but may affect performance, 2) C's manual memory management and compiler optimization are more efficient in recursive computing.

Golang is suitable for rapid development and concurrent scenarios, and C is suitable for scenarios where extreme performance and low-level control are required. 1) Golang improves performance through garbage collection and concurrency mechanisms, and is suitable for high-concurrency Web service development. 2) C achieves the ultimate performance through manual memory management and compiler optimization, and is suitable for embedded system development.

Golang and C each have their own advantages in performance competitions: 1) Golang is suitable for high concurrency and rapid development, and 2) C provides higher performance and fine-grained control. The selection should be based on project requirements and team technology stack.
