Understanding defer in Go

Written by Gopher Guides

Go has many of the common control-flow keywords found in other programming languages such as if, switch, for, etc. One keyword that isn’t found in most other programming languages is defer, and though it’s less common you’ll quickly see how useful it can be in your programs.

One of the primary uses of a defer statement is for cleaning up resources, such as open files, network connections, and database handles). When your program is finished with these resources, it’s important to close them to avoid exhausting the program’s limits and to allow other programs access to those resources. defer makes our code cleaner and less error prone by keeping the calls to close the file/resource in proximity to the open call.

In this article we will learn how to properly use the defer statement for cleaning up resources as well as several common mistakes that are made when using defer.

What is a defer Statement

A defer statement adds the function call following the defer keyword onto a stack. All of the calls on that stack are called when the function in which they were added returns. Because the calls are placed on a stack, they are called in last-in-first-out order.

Let’s look at how defer works by printing out some text:

main.go

  1. package main
  2. import "fmt"
  3. func main() {
  4. defer fmt.Println("Bye")
  5. fmt.Println("Hi")
  6. }

In the main function, we have two statements. The first statement starts with the defer keyword, followed by a print statement that prints out Bye. The next line prints out Hi.

If we run the program, we will see the following output:

Output

  1. Hi
  2. Bye

Notice that Hi was printed first. This is because any statement that is preceded by the defer keyword isn’t invoked until the end of the function in which defer was used.

Let’s take another look at the program, and this time we’ll add some comments to help illustrate what is happening:

main.go

  1. package main
  2. import "fmt"
  3. func main() {
  4. // defer statement is executed, and places
  5. // fmt.Println("Bye") on a list to be executed prior to the function returning
  6. defer fmt.Println("Bye")
  7. // The next line is executed immediately
  8. fmt.Println("Hi")
  9. // fmt.Println*("Bye") is now invoked, as we are at the end of the function scope
  10. }

The key to understanding defer is that when the defer statement is executed, the arguments to the deferred function are evaluated immediately. When a defer executes, it places the statement following it on a list to be invoked prior to the function returning.

Although this code illustrates the order in which defer would be run, it’s not a typical way it would be used when writing a Go program. It’s more likely we are using defer to clean up a resource, such as a file handle. Let’s look at how to do that next.

Using defer to Clean Up Resources

Using defer to clean up resources is very common in Go. Let’s first look at a program that writes a string to a file but does not use defer to handle the resource clean-up:

main.go

  1. package main
  2. import (
  3. "io"
  4. "log"
  5. "os"
  6. )
  7. func main() {
  8. if err := write("readme.txt", "This is a readme file"); err != nil {
  9. log.Fatal("failed to write file:", err)
  10. }
  11. }
  12. func write(fileName string, text string) error {
  13. file, err := os.Create(fileName)
  14. if err != nil {
  15. return err
  16. }
  17. _, err = io.WriteString(file, text)
  18. if err != nil {
  19. return err
  20. }
  21. file.Close()
  22. return nil
  23. }

In this program, there is a function called write that will first attempt to create a file. If it has an error, it will return the error and exit the function. Next, it tries to write the string This is a readme file to the specified file. If it receives an error, it will return the error and exit the function. Then, the function will try to close the file and release the resource back to the system. Finally the function returns nil to signify that the function executed without error.

Although this code works, there is a subtle bug. If the call to io.WriteString fails, the function will return without closing the file and releasing the resource back to the system.

We could fix the problem by adding another file.Close() statement, which is how you would likely solve this in a language without defer:

main.go

  1. package main
  2. import (
  3. "io"
  4. "log"
  5. "os"
  6. )
  7. func main() {
  8. if err := write("readme.txt", "This is a readme file"); err != nil {
  9. log.Fatal("failed to write file:", err)
  10. }
  11. }
  12. func write(fileName string, text string) error {
  13. file, err := os.Create(fileName)
  14. if err != nil {
  15. return err
  16. }
  17. _, err = io.WriteString(file, text)
  18. if err != nil {
  19. file.Close()
  20. return err
  21. }
  22. file.Close()
  23. return nil
  24. }

Now even if the call to io.WriteString fails, we will still close the file. While this was a relatively easy bug to spot and fix, with a more complicated function, it may have been missed.

Instead of adding the second call to file.Close(), we can use a defer statement to ensure that regardless of which branches are taken during execution, we always call Close().

Here’s the version that uses the defer keyword:

main.go

  1. package main
  2. import (
  3. "io"
  4. "log"
  5. "os"
  6. )
  7. func main() {
  8. if err := write("readme.txt", "This is a readme file"); err != nil {
  9. log.Fatal("failed to write file:", err)
  10. }
  11. }
  12. func write(fileName string, text string) error {
  13. file, err := os.Create(fileName)
  14. if err != nil {
  15. return err
  16. }
  17. defer file.Close()
  18. _, err = io.WriteString(file, text)
  19. if err != nil {
  20. return err
  21. }
  22. return nil
  23. }

This time we added the line of code: defer file.Close(). This tells the compiler that it should execute the file.Close prior to exiting the function write.

We have now ensured that even if we add more code and create another branch that exits the function in the future, we will always clean up and close the file.

However, we have introduced yet another bug by adding the defer. We are no longer checking the potential error that can be returned from the Close method. This is because when we use defer, there is no way to communicate any return value back to our function.

In Go, it is considered a safe and accepted practice to call Close() more than once without affecting the behavior of your program. If Close() is going to return an error, it will do so the first time it is called. This allows us to call it explicitly in the successful path of execution in our function.

Let’s look at how we can both defer the call to Close, and still report on an error if we encounter one.

main.go

  1. package main
  2. import (
  3. "io"
  4. "log"
  5. "os"
  6. )
  7. func main() {
  8. if err := write("readme.txt", "This is a readme file"); err != nil {
  9. log.Fatal("failed to write file:", err)
  10. }
  11. }
  12. func write(fileName string, text string) error {
  13. file, err := os.Create(fileName)
  14. if err != nil {
  15. return err
  16. }
  17. defer file.Close()
  18. _, err = io.WriteString(file, text)
  19. if err != nil {
  20. return err
  21. }
  22. return file.Close()
  23. }

The only change in this program is the last line in which we return file.Close(). If the call to Close results in an error, this will now be returned as expected to the calling function. Keep in mind that our defer file.Close() statement is also going to run after the return statement. This means that file.Close() is potentially called twice. While this isn’t ideal, it is an acceptable practice as it should not create any side effects to your program.

If, however, we receive an error earlier in the function, such as when we call WriteString, the function will return that error, and will also try to call file.Close because it was deferred. Although file.Close may (and likely will) return an error as well, it is no longer something we care about as we received an error that is more likely to tell us what went wrong to begin with.

So far, we have seen how we can use a single defer to ensure that we clean up our resources properly. Next we will see how we can use multiple defer statements for cleaning up more than one resource.

Multiple defer Statements

It is normal to have more than one defer statement in a function. Let’s create a program that only has defer statements in it to see what happens when we introduce multiple defers:

main.go

  1. package main
  2. import "fmt"
  3. func main() {
  4. defer fmt.Println("one")
  5. defer fmt.Println("two")
  6. defer fmt.Println("three")
  7. }

If we run the program, we will receive the following output:

Output

  1. three
  2. two
  3. one

Notice that the order is the opposite in which we called the defer statements. This is because each deferred statement that is called is stacked on top of the previous one, and then called in reverse when the function exits scope (Last In, First Out).

You can have as many deferred calls as needed in a function, but it is important to remember they will all be called in the opposite order they were executed.

Now that we understand the order in which multiple defers will execute, let’s see how we would use multiple defers to clean up multiple resources. We’ll create a program that opens a file, writes to it, then opens it again to copy the contents to another file.

main.go

  1. package main
  2. import (
  3. "fmt"
  4. "io"
  5. "log"
  6. "os"
  7. )
  8. func main() {
  9. if err := write("sample.txt", "This file contains some sample text."); err != nil {
  10. log.Fatal("failed to create file")
  11. }
  12. if err := fileCopy("sample.txt", "sample-copy.txt"); err != nil {
  13. log.Fatal("failed to copy file: %s")
  14. }
  15. }
  16. func write(fileName string, text string) error {
  17. file, err := os.Create(fileName)
  18. if err != nil {
  19. return err
  20. }
  21. defer file.Close()
  22. _, err = io.WriteString(file, text)
  23. if err != nil {
  24. return err
  25. }
  26. return file.Close()
  27. }
  28. func fileCopy(source string, destination string) error {
  29. src, err := os.Open(source)
  30. if err != nil {
  31. return err
  32. }
  33. defer src.Close()
  34. dst, err := os.Create(destination)
  35. if err != nil {
  36. return err
  37. }
  38. defer dst.Close()
  39. n, err := io.Copy(dst, src)
  40. if err != nil {
  41. return err
  42. }
  43. fmt.Printf("Copied %d bytes from %s to %s\n", n, source, destination)
  44. if err := src.Close(); err != nil {
  45. return err
  46. }
  47. return dst.Close()
  48. }

We added a new function called fileCopy. In this function, we first open up our source file that we are going to copy from. We check to see if we received an error opening the file. If so, we return the error and exit the function. Otherwise, we defer the closing of the source file we just opened.

Next we create the destination file. Again, we check to see if we received an error creating the file. If so, we return that error and exit the function. Otherwise, we also defer the Close() for the destination file. We now have two defer functions that will be called when the function exits its scope.

Now that we have both files open, we will Copy() the data from the source file to the destination file. If that is successful, we will attempt to close both files. If we receive an error trying to close either file, we will return the error and exit function scope.

Notice that we explicitly call Close() for each file, even though the defer will also call Close(). This is to ensure that if there is an error closing a file, we report the error. It also ensures that if for any reason the function exits early with an error, for instance if we failed to copy between the two files, that each file will still try to close properly from the deferred calls.

Conclusion

In this article we learned about the defer statement, and how it can be used to ensure that we properly clean up system resources in our program. Properly cleaning up system resources will make your program use less memory and perform better. To learn more about where defer is used, read the article on Handling Panics, or explore our entire How To Code in Go series.