Home  >  Article  >  Backend Development  >  Detailed introduction to the key features of defer in golang

Detailed introduction to the key features of defer in golang

巴扎黑
巴扎黑Original
2017-08-12 14:41:561424browse

defer is a keyword in the golang language, which is used to release resources and will be called before the function returns. The following article mainly introduces you to the key features of defer in golang. The article introduces it in detail through sample code, which has certain reference and learning value for everyone. Friends who need it can take a look below.

Preface

Everyone knows golang’s defer keyword, which can perform some operations before the function returns. The most commonly used one is to open a When using a resource (such as a file, database connection, etc.), use defer to delay closing the resource to avoid memory leaks. This article mainly introduces to you the key features of defer in golang, and shares it for your reference and learning. Without further ado, let’s take a look at the detailed introduction:

1. defer The role and execution timing

go's defer statement is used to delay the execution of the function, and the delay occurs after calling the function return, such as

## The execution of
#

func a() int {
 defer b()
 return 0
}

b occurs after return 0. Pay attention to the syntax of defer. The keyword defer is followed by the function call.

2. Important uses of defer 1: Cleaning up and releasing resources

Due to the delay characteristics of defer, defer is often used at the end of a function call Then clean up related resources, such as


f, _ := os.Open(filename)
defer f.Close()

The release of file resources will be automatically executed with the help of defer after the function call is completed. There is no need to always remember where the resources need to be released. Open must correspond to release.

Use an example to deeply explain the convenience and simplicity brought by defer.

The main purpose of the code is to open a file and then copy the content to another new file. If there is no defer, write like this:


func CopyFile(dstName, srcName string) (written int64, err error) {
 src, err := os.Open(srcName)
 if err != nil {
  return
 }
 dst, err := os.Create(dstName)
 if err != nil { //1
  return
 }
 written, err = io.Copy(dst, src)
 dst.Close()
 src.Close()
 return
}

The code is in After returning at #1, the src file is not closed, which may result in resources not being released correctly. Use defer instead:


func CopyFile(dstName, srcName string) (written int64, err error) {
 src, err := os.Open(srcName)
 if err != nil {
  return
 }
 defer src.Close()
 dst, err := os.Create(dstName)
 if err != nil {
  return
 }
 defer dst.Close()
 return io.Copy(dst, src)
}

Both src and dst can be cleaned up and processed in time. Release, no matter where return is executed.

In view of the role of defer, defer is often used to release database connections, file open handles and other operations to release resources.

3. Important uses of defer 2: execute recovery

The defer function is executed after return. This is the right time. Panic thrown by a function can be captured, so another important use of defer is to perform recovery.

recover is only more meaningful when used in defer. If used elsewhere, the error cannot be effectively captured because the program has already been called and returns early.


package main
import (
 "fmt"
)
func main() {
 defer func() {
  if ok := recover(); ok != nil {
   fmt.Println("recover")
  }
 }()
 panic("error")
}

Remember defer should be placed before panic execution.

4. The execution sequence of multiple defers

The function of defer is to push the function execution after the keyword into a stack Medium delayed execution, the execution order of multiple defers is last-in-first-out LIFO:


defer func() { fmt.Println("1") }()
defer func() { fmt.Println("2") }()
defer func() { fmt.Println("3") }()

The output order is 321.

This feature can implement reverse order operations on an array.

5. The parameters of the deferred function are determined when defer

This is the characteristic of defer. When a function is defer, Its parameters are calculated and determined when deferred. Even if the parameters are modified after defer, it will have no effect on the function that has been defered. What does this mean? Look at the example:


func a() {
 i := 0
 defer fmt.Println(i)
 i++
 return
}

a The execution output is 0 instead of 1, because when defer, the value of i is 0, and the function parameters being defer have already been executed and calculated. and confirmed.

Look at another example:


func calc(index string, a, b int) int {
 ret := a + b
 fmt.Println(index, a, b, ret)
 return ret
}
func main() {
 a := 1
 b := 2
 defer calc("1", a, calc("10", a, b))
 a = 0
 return
}

Execution code output


10 1 2 3 
1 1 3 4

defer function parameters The third parameter has been calculated and determined when deferred, and the same is true for the second parameter a. It does not affect whether the a variable is modified later.

6. The defer function can read and modify the return value with name


func c() (i int) {
 defer func() { i++ }()
 return 1
}

The defer function is executed after return, and the return value with name can be modified. The above function c returns 2.

The above is the detailed content of Detailed introduction to the key features of defer in golang. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn