This is a creation in Article, where the information may have evolved or changed.
Preview Catalog
- Some conclusions
- Closures and anonymous functions
- Consolidate
- Reference documents
What is defer? How do I understand defer keywords? Go to use some of the pits in defer.
Defer means delay, which is used in Golang to delay the execution of a function. It can help us deal with issues that are easy to ignore, such as resource release, connection shutdown, and so on. But in the actual use of the process, there are some areas to be aware of (pit), below us one by one.
Some conclusions
First, let's take a look at some of defer's conclusions:
1, if there are multiple defer in the function, its execution order is advanced , can be understood as a stack.
package mainimport "fmt"func main() { for i := 0; i < 5; i++ { defer fmt.Println(i) }}Output:43210
2, return will do a few things:
- Assigning a value to a return value
- Call defer expression
- Return to the calling function
package mainimport "fmt"func main() { fmt.Println(increase(1))}func increase(d int) (ret int) { defer func() { ret++ }() return d} Output:2
3, if the defer expression has a return value, it will be discarded.
Please refer to the official documentation for more information.
Closures and anonymous functions
anonymous function: A function without a function name.
Closure: A function that can use a variable in another function scope.
In practical development, the use of defer is often accompanied by the use of closures and anonymous functions. Take care of the pit!
package mainimport "fmt"func main() { for i := 0; i < 5; i++ { defer func() { fmt.Println(i) }() }}Output:55555
To explain, the defer expression is a i
reference to the For loop i
. To the end, I plus to 5, so the last all print 5.
If i
it is passed as a parameter into the defer expression, it will be evaluated at the beginning of the pass-through, but no delay function is executed.
for i := 0; i < 5; i++ { defer func(idx int) { fmt.Println(idx) }(i) // 传入的 i,会立即被求值保存为 idx}
Consolidate
To solidify the above knowledge point, let's consider a few examples.
Example 1:
func f() (result int) { defer func() { result++ }() return 0}
Example 2:
func f() (r int) { t := 5 defer func() { t = t + 5 }() return t}
Example 3:
func f() (r int) { defer func(r int) { r = r + 5 }(r) return 1}
Did you get a result? The answer to Example 1 is not 0, the answer to Example 2 is not 10, and the answer to example 3 is not 6.
Example 1, relatively simple, refer to conclusion 2, assign 0 to the Result,defer delay function to modify result, and finally return to the calling function. The correct answer is 1.
Example 2,defer is performed after the T assignment to r, whereas the defer delay function only changes the value of T and R remains unchanged. Correct answer 5.
Example 3, where R is passed as a parameter to the defer expression. Therefore, R in R is not the same as the func (r int)
func f() (r int)
parameter name. Correct answer 1.
Reference documents
[1] https://tiancaiamao.gitbooks.io/go-internals/content/zh/03.4.html
[2] Http://golang.org/ref/spec#defer_statements
This article links: https://deepzz.com/post/how-to-use-defer-in-golang.html, participating in the comments»
--eof--
Posted in 2017-08-27 02:08:00.
This site uses the "Signature 4.0 International" Creative sharing agreement, reproduced please specify the author and the original website. More Instructions»