Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of defer in Go

Tags:

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?

like image 462
Shubham Chaudhary Avatar asked Dec 02 '17 12:12

Shubham Chaudhary


People also ask

What is the use of Defer in Swift?

The Swift defer statement is useful for cases where we need something done — no matter what — before exiting the scope. For example, defer can be handy when cleanup actions are performed multiple times, like closing a file or locking a lock, before exiting the scope.

Can we return in defer Golang?

defer statement is a convenient way to execute a piece of code before a function returns, as explained in Golang specification: Instead, deferred functions are invoked immediately before the surrounding function returns, in the reverse order they were deferred.

Does defer run on panic Golang?

In Go, we use defer, panic and recover statements to handle errors. We use defer to delay the execution of functions that might cause an error. The panic statement terminates the program immediately and recover is used to recover the message during panic.

Is defer a Goroutine?

When a defer statement is executed, the deferred function call is not executed immediately. Instead, it is pushed into a deferred call queue maintained by its caller goroutine.


2 Answers

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()) } 
like image 175
Grzegorz Żur Avatar answered Oct 17 '22 17:10

Grzegorz Żur


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.

like image 21
Mayur Avatar answered Oct 17 '22 18:10

Mayur