This is a creation in Article, where the information may have evolved or changed.
Reprint: http://cloudnil.com/2014/05/19/defer-in-go/
There is no block of statements in the go language similar to try...catch...finally in Java, but there is a very elegant defer.
The defer keyword is used to mark the last GO statement, which is typically used in resource releases, closing connections, and so on, before the function is closed.
Multiple defer are defined with the execution of a stack-like operation: Advanced-out, first-defined final execution.
In the use of defer, encountered a lot of pits, especially in defer and anonymous functions with the use of the time, this article is about these pits.
Please look at the following code, and then determine the respective output:
| 1234567891011121314151617181920212223242526272829303132 |
#示例代码一:func funcA() int { x := 5 defer func() { x += 1 }() return x}#示例代码二:func funcB() (x int) { defer func() { x += 1 }() return 5}<!--more-->#示例代码三:func funcC() (y int) { x := 5 defer func() { x += 1 }() return x}#示例代码四:func funcD() (x int) { defer func(x int) { x += 1 }(x) return 5} |
Run the code for a try, and I'm sure a lot of readers will find their answers wrong.
Parsing these pieces of code, the main need to understand the following knowledge:
1, the return statement processing process
The return XXX statement is not an atomic instruction, and it is executed with the statement decomposed into the return variable =xxx return, and the last execution of the return
2. Defer statement Execution time
As mentioned above, the defer statement is called when the function is closed, and is actually called when the return statement is executed, note that return is not return XXX
3. How to transfer function parameters
In the go language, the normal function parameter is passed in the value pass, that is, the new memory copy variable value, excluding slice and map, these two types are reference passing
4. How to transfer variable assignment
The assignment of the Go language variable is similar to the function parameter and is also a value copy, excluding slice and map, which are memory references
In accordance with the above principles, parse the code:
| 123456789 |
#示例代码一: Func Funca () int { x: = 5 temp=x #temp变量表示未显示声明的return变量 &NBSP;&NBSP;&NBSP;&NBSP; func () { x + = 1 &NBSP;&NBSP;&NBSP;&NBSP; } () return } |
Returns the value of temp, after assigning X to temp, the temp has not changed and the final return value is 5
| 12345678 |
#示例代码二: func FUNCB () (x int) { &NBSP;&NBSP;&NBSP;&NBSP; Code class= "Plain Plain" style= "" >x = 5 &NBSP;&NBSP;&NBSP;&NBSP; Code class= "Plain Plain" style= "" >func () { x + = 1 &NBSP;&NBSP;&NBSP;&NBSP; } () return } |
Return the value of x, copy 5 first, then change to 6 in the function, and the final return value is 6
| 123456789 |
#示例代码三: Func FUNCC () (y int) { x: = 5 y = x #这里是值拷贝 func () { &NBSP;&NBSP;&NBSP;&NBSP; } () return " Code class= "Plain Plain" style= "" >} |
Returns the value of Y, after assigning x to Y, Y has not changed and the final return value is 5
| 12345678 |
#示例代码四: func Funcd () (x int) { &NBSP;&NBSP;&NBSP;&NBSP; Code class= "Plain Plain" style= "" >x: = 5 func (x int) {#这里是值拷贝 x + = 1 &NBSP;&NBSP;&NBSP;&NBSP; } (x) return } |
Returns the value of x, passing x to the anonymous function when executed, passing the copy of X, whose internal modification does not affect the value of the external X, the final return value is 5
It can be seen that the defer statements are inserted into the decomposition of the return statement before execution, combined with assignment and parameter value transfer, you can clearly understand the final result.