This is a creation in Article, where the information may have evolved or changed.
Go language defer, do you really understand?
2013-04-25
Example1
func f() (result int) { defer func() { result++ }() return 0}
Example2
func f() (r int) { t := 5 defer func() { t = t + 5 }() return t}
Example3
func f() (r int) { defer func(r int) { r = r + 5 }(r) return 1}
Don't run the code first, run the results in your heart, and then verify. If three are all right and not good. Well, don't look down, you already know the defer.
Well, if you count 0 in example1, you're wrong, and if example2 you think it's 10, you're wrong ... The right not to count ... If you feel 6 in Example3, you are wrong again ... If you are right, there is a mistake, OK ... You are the one who is blindfolded! Do not understand the continue to look down ah ...
The first thing to be clear is that defer is executed before return. This is clearly stated in the official documentation.
Then it is to understand how the defer is implemented, roughly where the defer appears, insert the command call Runtime.deferproc, and then insert the command call Runtime.deferreturn where the function is before it returns. And then the explicit go return value is not the same as C, in order to support multi-value return, GO is the stack return value, and C is the register.
The most important point is:
return XXX This statement is not an atomic instruction!
The entire return process, before defer, writes a value in the stack, which is treated as a return value and then returned by calling the RET instruction. return XXX statement After the assembly is to give the value of the return value, then make an empty return, (Assignment instruction + ret instruction). The execution of the defer is inserted before the return instruction, and with defer, it becomes the (Assignment instruction + call defer instruction + RET instruction). In the call defer function, it is possible to rewrite the final return value ... It may not have been rewritten. In short, if it is rewritten, it looks like defer is executed after return xxx ~
This is the root of all the defer stories that you think you don't understand.
The basics are there, and then you can talk about the magical defer. Tell us a simple conversion rule that everyone will never be confused for defer. The rewrite rule is to separate the return statement into two sentences, and return XXX will be rewritten as:
返回值 = xxx调用defer函数空的return
Look at example1 first, it can be rewritten like this:
func f() (result int) { result = 0 //return语句不是一条原子调用,return xxx其实是赋值+RET指令 func() { //defer被插入到return之前执行,也就是赋返回值和RET指令之间 result++ }() return}
So this return value is 1.
Look at example2 again, it can be rewritten like this:
func f() (r int) { t := 5 r = t //赋值指令 func() { //defer被插入到赋值与返回之间执行,这个例子中返回值r没被修改过 t = t + 5 } return //空的return指令}
So the result of this is 5.
Finally look at the Example3, it rewritten to become:
func f() (r int) { r = 1 //给返回值赋值 func(r int) { //这里改的r是传值传进去的r,不会改变要返回的那个r值 r = r + 5 }(r) return //空的return}
So the result of this example is 1.
Do you understand me?
Conclusion: Defer is actually called before the return. But the form of expression may not be like. The essential reason is that the return XXX statement is not an atomic instruction, defer is inserted before the assignment and RET, so there may be a chance to change the final return value. When you feel confused, you can use the set of rules I gave to turn the code.