How does the defer keyword in Golang actually work - go

I had this code:
defer common.LogWarning(
"b09ee123-f18b-46a8-b80d-f8361771178d:",
resp.Body.Close(), // gets called immediately, is *not* deferred..
)
and common.LogWarning is simply like this:
func LogWarning(uuid string, err error) {
if err != nil {
log.Warning(uuid, err)
}
}
the problem is that resp.Body.Close() gets called immediately - that call is not deferred, so how does this work? Why is not the whole code block get deferred?

From the documentation:
The behavior of defer statements is straightforward and predictable. There are three simple rules:
A deferred function's arguments are evaluated when the defer statement is evaluated.

The defer statement defers the function call. The arguments to the function are evaluated immediately.
Use an anonymous function to accomplish your goal:
defer func() {
common.LogWarning("b09ee123-f18b-46a8-b80d-f8361771178d:",
resp.Body.Close())
}()
The call represented by the trailing () is deferred.

defer defers the execution of a function until the current function returns. The arguments to the function are evaluated immediately.
https://tour.golang.org/flowcontrol/12
If you need to defer a code block where all evaluations are to be deferred, make it a function:
defer func() {
// Stuff to defer here
}()
``

Related

How does a caller function to recover from child goroutine's panics

I used to think the panic in a goroutine will kill the program if its caller finishes before the panic (the deferred recovering gives no help since at that point there's no panic occurs yet),
until I tried following code:
func fun1() {
fmt.Println("fun1 started")
defer func() {
if err := recover(); err != nil {
fmt.Println("recover in func1")
}
}()
go fun2()
time.Sleep(10 * time.Second) // wait for the boom!
fmt.Println("fun1 ended")
}
func fun2() {
fmt.Println("fun2 started")
time.Sleep(5 * time.Second)
panic("fun2 booom!")
fmt.Println("fun2 ended")
}
I found no matter the caller function finishes or not, if the goroutines it starts panic, the caller's deferred recover mechanism will not help. The whole program is still dead.
So, WHY? Theoretically the caller function is still running. When the panics happen the caller's deferred functions should work (including the recovering).
The specification says:
While executing a function F, an explicit call to panic or a run-time panic terminates the execution of F. Any functions deferred by F are then executed as usual. Next, any deferred functions run by F's caller are run, and so on up to any deferred by the top-level function in the executing goroutine. At that point, the program is terminated and the error condition is reported, including the value of the argument to panic. This termination sequence is called panicking.
Because fun2 is the top-level function executing in the goroutine and fun2 does not recover from a panic, the program terminates when fun2 panics.
The deferred call in fun1 is not called when the goroutine executing fun2 panics.
A goroutine cannot recover from a panic in another goroutine.
Instead of recovering in fun1() you can use runtime.Goexit() in fun2() which will
Goexit terminates the goroutine that calls it. No other goroutine is
affected.
Something like
func fun2() {
defer func() {
if err := recover(); err != nil {
fmt.Println("Do some cleanup and teardown")
runtime.Goexit() //Here
}
}
...
}

Use of defer in Go

What is the use of defer in Go? The language documentation says it is executed when the surrounding function returns. Why not just put the code at end of given function?
We usually use defer to close or deallocate resources.
A surrounding function executes all deferred function calls before it returns, even if it panics. If you just place a function call at the end of a surrounding function, it is skipped when panic happens.
Moreover a deferred function call can handle panic by calling the recover built-in function. This cannot be done by an ordinary function call at the end of a function.
Each deferred call is put on stack, and executed in reverse order when the surrounding function ends. The reversed order helps deallocate resources correctly.
The defer statement must be reached for a function to be called.
You can think of it as another way to implement try-catch-finally blocks.
Closing like try-finally:
func main() {
f, err := os.Create("file")
if err != nil {
panic("cannot create file")
}
defer f.Close()
// no matter what happens here file will be closed
// for sake of simplicity I skip checking close result
fmt.Fprintf(f,"hello")
}
Closing and panic handling like try-catch-finally
func main() {
defer func() {
msg := recover()
fmt.Println(msg)
}()
f, err := os.Create(".") // . is a current directory
if err != nil {
panic("cannot create file")
}
defer f.Close()
// no matter what happens here file will be closed
// for sake of simplicity I skip checking close result
fmt.Fprintf(f,"hello")
}
The benefit over try-catch-finally is that there is no nesting of blocks and variable scopes. This simplifies the structure of the surrounding function.
Just like finally blocks, deferred function calls can also modify the return value if they can reach the returned data.
func yes() (text string) {
defer func() {
text = "no"
}()
return "yes"
}
func main() {
fmt.Println(yes())
}
There are already good answers here. I would like to mention one more use case.
func BillCustomer(c *Customer) error {
c.mutex.Lock()
defer c.mutex.Unlock()
if err := c.Bill(); err != nil {
return err
}
if err := c.Notify(); err != nil {
return err
}
// ... do more stuff ...
return nil
}
The defer in this example ensures that no matter how BillCustomer returns, the mutex will be unlocked immediately prior to BillCustomer returning. This is extremely useful because without defer you would have to remember to unlock the mutex in every place that the function could possibly return.
ref.
Well, it's not always guaranteed that your code may reach the end of the function (e.g. an error or some other condition may force you to return well ahead of the end of a function). The defer statement makes sure that whatever function is assigned to it gets executed for sure even if the function panics or the code returns well before the end of the function.
The defer statement also helps keep the code clean esp. in cases when there are multiple return statements in a function esp. when one needs to free resources before return (e.g. imagine you have an open call for accessing a resource at the beginning of the function - for which a corresponding close must be called before the function returns for avoiding a resource leak. And say your function has multiple return statements, maybe for different conditions including error checking. In such a case, without defer, you normally would call close for that resource before each return statement). The defer statement makes sure the function you pass to it is always called irrespective of where the function returns, and thus saves you from extraenous housekeeping work.
Also defer can be called multiple times in the same function. E.g.: In case you have different resources being allocated through your function which need to be eventually freed before returning, then you can call defer for each of them after allocation and these functions are executed in the reverse order of the sequence in which they were called when the function exits.
Key benefit of using defer - it will be called any way no matter how function will return. If an extraordinary situation would occur deferred function will be called.
So it gives nice things:
Recover after panic. This allows yes realize try ... catch behavior.
Not to forget clean up (close files, free memory, etc) before normal exit. You may open some resource and you have to close it before exit. But function can have several exit points - so you have to add freeing in every return point. That’s very tedious in maintenance. Or you can put only one deferred statement - and resources will be released automatically.
Summary:
When we do certain operations that need cleanup, we can "schedule" the cleanup operations which would be run when the function returns no matter which path that happens, including due to panic.
Detailed answer:
Programming languages strive to provide constructs that facilitate simpler and less error-prone development. (E.g. why should Golang support garbage collection when we can free the memory ourselves)
A function can return at multiple points. The user might overlook doing certain cleanup operations in some paths
Some cleanup operations are not relevant in all return paths
Also, it is better to keep the cleanup code closer to the original operation which needed the cleanup
When we do certain operations that need cleanup, we can "schedule" the cleanup operations which would be run when the function returns no matter which path that happens.
A defer statement defers the execution of a function until the
surrounding function returns.
This example demonstrates defer functionality:
func elapsed(what string) func() {
start := time.Now()
fmt.Println("start")
return func() {
fmt.Printf("%s took %v\n", what, time.Since(start))
}
}
func main() {
defer elapsed("page")()
time.Sleep(time.Second * 3)
}
Out:
start
page took 3s

I defer a function return function, what is the order

I write the follow code
package main
import "fmt"
func main() {
defer func() func() {
fmt.Println("start")
return func() {
fmt.Println("end")
}
}()()
fmt.Println("aaaa")
return
}
and I except output is aaaa start end
but actual output is start aaaa end
I can't understand why output "start" before "aaaa"
The specification says:
Each time a "defer" statement executes, the function value and parameters to the call are evaluated as usual and saved anew but the actual function is not invoked.
The deferred function call is the last () in the defer statement. The expression returning the function value is evaluated at the time of the defer statement.
Since defer statement needs to evaluate the statement, in your code, the func() (the func() right after "defer" keyword) returns a function type, defer statement needs to actually execute the func() to get the return function. So your code prints out "start" first.
If your function does not return a function type, then the function body will not be executed until enclosing function returns.

Why does `defer recover()` not catch panics?

Why does a call to defer func() { recover() }() successfully recover a panicking goroutine, but a call to defer recover() not?
As an minimalistic example, this code doesn't panic
package main
func main() {
defer func() { recover() }()
panic("panic")
}
However, replacing the anonymous function with recover directly panics
package main
func main() {
defer recover()
panic("panic")
}
Quoting from the documentation of the built-in function recover():
If recover is called outside the deferred function it will not stop a panicking sequence.
In your second case recover() itself is the deferred function, and obviously recover() does not call itself. So this will not stop the panicking sequence.
If recover() would call recover() in itself, it would stop the panicking sequence (but why would it do that?).
Another Interesting Example:
The following code also doesn't panic (try it on the Go Playground):
package main
func main() {
var recover = func() { recover() }
defer recover()
panic("panic")
}
What happens here is we create a recover variable of function type which has a value of an anonymous function calling the built-in recover() function. And we specify calling the value of the recover variable to be the deferred function, so calling the builtin recover() from that stops the panicing sequence.
The Handling panic section mentions that
Two built-in functions, panic and recover, assist in reporting and handling run-time panics
The recover function allows a program to manage behavior of a panicking goroutine.
Suppose a function G defers a function D that calls recover and a panic occurs in a function on the same goroutine in which G is executing.
When the running of deferred functions reaches D, the return value of D's call to recover will be the value passed to the call of panic.
If D returns normally, without starting a new panic, the panicking sequence stops.
That illustrates that recover is meant to be called in a deferred function, not directly.
When it panic, the "deferred function" cannot be the built-in recover() one, but one specified in a defer statement.
DeferStmt = "defer" Expression .
The expression must be a function or method call; it cannot be parenthesized.
Calls of built-in functions are restricted as for expression statements.
With the exception of specific built-in functions, function and method calls and receive operations can appear in statement context.
An observation is that the real problem here is the design of defer and thus the answer should say that.
Motivating this answer, defer currently needs to take exactly one level of nested stack from a lambda, and the runtime uses a particular side effect of this constraint to make a determination on whether recover() returns nil or not.
Here's an example of this:
func b() {
defer func() { if recover() != nil { fmt.Printf("bad") } }()
}
func a() {
defer func() {
b()
if recover() != nil {
fmt.Printf("good")
}
}()
panic("error")
}
The recover() in b() should return nil.
In my opinion, a better choice would have been to say that defer takes a function BODY, or block scope (rather than a function call,) as its argument. At that point, panic and the recover() return value could be tied to a particular stack frame, and any inner stack frame would have a nil pancing context. Thus, it would look like this:
func b() {
defer { if recover() != nil { fmt.Printf("bad") } }
}
func a() {
defer {
b()
if recover() != nil {
fmt.Printf("good")
}
}
panic("error")
}
At this point, it's obvious that a() is in a panicking state, but b() is not, and any side effects like "being in the first stack frame of a deferred lambda" aren't necessary to correctly implement the runtime.
So, going against the grain here: The reason this doesn't work as might be expected, is a mistake in the design of the defer keyword in the go language, that was worked around using non-obvious implementation detail side effects and then codified as such.

Golang goroutine not running defer with function that uses range on channel

A range on a channel
for s := range cs {
fmt.Println("Recieved Cake: ", s)
}
should keep a function open until the channel closes at which point the function/goroutine should terminate. When a function terminates the defer function should run just prior. This doesn't seem to be the case and I can't find any reasons why.
Sample code at http://play.golang.org/p/ADu1MzAe9P produces defer statements as expected except for the function that is recieving from the channel. Any reasons as to why this would be? thanks!
The reason why the defer function is not executing is that the application reaches the end of the main function causing the entire program to terminate without waiting for goroutines.
Go Specification says:
When the function main returns, the program exits. It does not wait for other (non-main) goroutines to complete.
Since your recieveCakeAndPack is still waiting for the channel to close (which never happens) it will never defer before the termination of the program.
Edit
On a side note - putting the defer statements last in a function is not meaningful. Instead put them directly after the statement you want to defer such as:
fmt.Println("Entering function")
defer fmt.Println("Leaving function")
or
file, err := os.Open("file.txt")
if err != nil {
return err
}
defer file.Close()
The defer function/method calls will be executed when leaving the function in a Last-In-First-Out order.

Resources