Defer of statement series

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

First, defer key words work principle

1. A deferred function ' s arguments was evaluated when the defer statement was evaluated.

func a() {    i := 0    defer fmt.Println(i)    i++    return}

The variable i in the Defer function is clear when the defer function is defined, with a value of 0. Subsequent i++ outside of defer will not affect the defer function, so the result is: 0.

2.Deferred function calls is executed in last on first out order after the surrounding function returns.

func b() {    for i := 0; i < 4; i++ {        defer fmt.Print(i)    }}

The order in which multiple defer functions are called follows the principle of last-in, first-out , so the print result is: 3210.

3.Deferred functions may read and assign to the returning function ' s named return values.

func c() (i int) {    defer func() { i++ }()    return 1}

The defer function can read and assign a named return value to an external function. So the return value of function C is: 2.

Ii. defer return execution process

func f0() (r int) {    t := 5    defer func() {        fmt.Printf("f0 defer t is :%d", t)//5        fmt.Printf("f0 defer r is :%d", r)//5        t = t + 5        fmt.Printf("f0 defer r is :%d", r)//5    }()    return t}

It is important to note that the return xxx statement is not an atomic instruction, and the defer is inserted between the assignment and the RET. Therefore return t should be disassembled two parts, r = t and return .

So the F0 function can be transformed into the following function

func f0() (r int) {    t := 5    r = t    defer func() {        fmt.Printf("f0 defer t is :%d", t)//5        fmt.Printf("f0 defer r is :%d", r)//5        t = t + 5        fmt.Printf("f0 defer r is :%d", r)//5    }()    return}

The return value of the final function F0 is: 5.

Third, the thinking test

func f1() (r int) {    defer func(r int) {        fmt.Printf("f1 defer r is:%d", r)//0        r = r + 5        fmt.Printf("f1 defer2 r is:%d", r)//5    }(r)    fmt.Printf("f1 r is:%d", r)//0    return 1}

function return Result: 1. (Defer three principle, value transfer, return XXX disassembly)

func f2() (r int) {    defer func() {        fmt.Printf("f2 defer r is:%d", r)//1        r = r + 5        fmt.Printf("f2 defer2 r is:%d", r)//6    }()    fmt.Printf("f2 r is:%d", r)//0    return 1}

The function returns a result of: 6. (Refer three principle, return XXX disassembly)

Resources

Defer, Panic, and Recover

Defer statements

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.