Go language defer, do you really understand?

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

2013-04-25

Go language defer, do you really understand?

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 directive!*

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:

return value = XXX Call defer function 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 the return execution, That is, the assignment return value and the RET instruction between         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//assignment Instruction     func () {        //defer is inserted between assignment and return execution, 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 look at the Example3, it rewritten to become:

Func f () (r int) {     R = 1  //assigns the return value     func (r int) {        //here R is a value passed in R, does not change the R value to be returned r          = R + 5     } (r) 
  return        //Empty 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.

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.