Introduction Golang中的defer语句可以用来在函数结束时执行一些清理工作,它具有类似于C++中RAII的功能,经常用于释放资源、关闭文件等操作。不过,在实际使用中,如果不小心使用defer可能会导致性能问题。本文将介绍一些使用defer时需要小心的地方以及如何避免这些问题。 When should we use defer? Before we dive into the pitfalls of using defer, let's first discuss when and why we use defer. We should use defer when we want to ensure that a function call is performed later in a program's execution, regardless of whether the function that contains the defer statement exits normally or abnormally. For example, if we open a file in a function and want to ensure that it is closed before the function returns, we can use defer to register a call to the close function, like this: ``` func readFile(filename string) ([]byte, error) { f, err := os.Open(filename) if err != nil { return nil, err } defer f.Close() // ensure that file is closed before function returns ... } ``` In this example, the `f.Close()` function call will be executed before the function returns, regardless of whether the return is caused by a `return` statement or an error. Now that we understand the basic usage of defer, let's look at some of the pitfalls we should avoid when using it. Pitfall 1: Deferring function calls in loops One common mistake when using defer is to defer a function call in a loop. Consider the following code: ``` func problematic() { for i := 0; i < 10; i++ { defer fmt.Println(i) } } ``` This code prints the numbers from 9 to 0, instead of from 0 to 9, because the `defer fmt.Println(i)` statement defers the function call until the end of the function, when `i` has already been incremented to 10. This mistake can lead to unexpected behavior and should be avoided. Pitfall 2: Deferring expensive function calls Another pitfall of using defer is to defer expensive function calls. Consider the following code: ``` func problematic(filePath string) error { f, err := os.Open(filePath) if err != nil { return err } // read entire file contents into memory defer func() { data, _ := ioutil.ReadAll(f) fmt.Println(string(data)) }() // do some other work here... return nil } ``` In this code, we use defer to read the entire file contents into memory and print them out when the function returns. However, this can be very expensive if the file is large, and can lead to performance issues. In general, we should avoid deferring expensive function calls and instead call them directly when they are needed. Pitfall 3: Deferring function calls with arguments Finally, another pitfall of using defer is to defer function calls with arguments. Consider the following code: ``` func problematic() { for i := 0; i < 10; i++ { defer fmt.Println("deferred", i) } } ``` This code prints the same thing as the previous example, because the arguments to the function call are evaluated immediately when the `defer` statement is executed, not when the function is called. To avoid this problem, we can use a closure to capture the variables we want to defer, like this: ``` func working() { for i := 0; i < 10; i++ { defer func(i int) { fmt.Println("deferred", i) }(i) } } ``` In this code, we create a closure that captures `i` and passes it as an argument to the deferred function call. This ensures that the correct value of `i` is printed out when the function is called. Conclusion In conclusion, defer is a powerful feature in Golang that can be used to ensure that a function call is performed at the end of a function, regardless of whether the function exits normally or abnormally. However, when using defer, we should be careful to avoid some common pitfalls, such as deferring function calls in loops, deferring expensive function calls, and deferring function calls with arguments. By being aware of these issues, we can use defer effectively without introducing performance problems into our code.