Analysis of defer in Golang

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

This article goes from Chen Yuwen's blog: http://my.oschina.net/yuwenc/blog/300592

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 mind again. and then to verify. If three are all right and not blindfolded .... Well, don't look down, you already know the defer.




A few more lines make sure you run through the code first in your heart, then verify, and there's doubt ...

Well, if you're 0 in example1, you're wrong.

If you think it's 10 in example2, 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 thatdefer is executed before return

This is the official document of the Http://golang.org/ref/spec#Defer_statements clearly stated, know on the line, can ignore


And then to understand the way that defer was implemented, I've written about http://bbs.mygolang.com/thread-271-1-1.html before .

The idea is to insert an instruction in the place where the defer appears.

Call Runtime.deferproc

Then, before the function returns, insert the instruction

Call Runtime.deferreturn


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 sentence is not an atomic directive!

The entire return process, no defer before, is to write a value in the stack, this value will be treated as the return value. The RET instruction is then called back. return XXX statement After the assembly is to give the value of the returned value, and then make an empty return: (Assignment instruction + RET command)

The execution of the defer is inserted before the return instruction.

With defer, it becomes (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 say the magic 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:

return value = XXX

Call the Defer function

An empty return


Look at example1 first. It can be rewritten like this:

Func f () (result int) {

result = 0//return statement is not an atomic call, return XXX is actually an assignment +ret instruction

Func () {//defer is inserted before return, which is the assignment of the return value and the RET instruction

result++

}()

Return

}

So this return is 1.


Look at example2 again. It can be rewritten like this:

Func f () (r int) {

T: = 5

R = t//assignment instruction

Func () {//defer is inserted between the assignment and the return execution, and the return value R in this example has not been modified

t = t + 5

}

return//NULL return instruction

}

So the result of this is 5.


finally see Example3. It is rewritten and then becomes:

Func f () (r int) {

R = 1//Assign value to return value

Func (r int) {//R here is a value passed in R, does not change the R value to be returned

R = R + 5

} (R)

return//Empty return

}

So this example turns out to be 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.






Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.