Use the GO language study notes 5-defer the

1. What is defer

defer registration delay is a mechanism for providing language called Go: Let the function or statement (including the normal end or abnormal end of the panic caused by the return) after the current function completes execution.

2. defer application scenarios

statements are intended to defer some scenes pair operations: connecting opening / closing the connection; lock / release latch; file open / close files.

3. defer principle of

Defer implementation of the statement will not be executed immediately, but will enter a stack before the function return, will press last-out (FILO) order. That statement is the first to be defined defer the final execution. The reason, last-out function is defined later may depend on the resources front, the natural first execution; otherwise, if the previous execution first, then rely on the back, there is no function.

4. defer the reference

When defer statement defines, for the following two external variables by reference:

  • Incorporated as a function of parameters and closures. As a function of the parameter, the value is passed to defer put at defer defined and cached.
  • Referenced as the package closed, then it determines the current value when the function actually invoked according to defer the whole context.

5. defer stepped pit

The key to avoiding stepping pit is to understand this statement:

return xxx

This statement compiled after, may become three instructions:

1. 返回值 = xxx
2. 调用defer函数
3. 空的return

Step 3 is the return statement is true command, step 2 is defer the definition statement, there is likely to be operating return value.

Code Example 6

Example 1:
func f() (r int) {
    defer func() {
        r++
    }()
    return 0
}

Dismantling process:

func f() (r int) {

    // 1.赋值
    r = 0

    // 2.闭包引用,返回值被修改
    defer func() {
        r++
    }()

    // 3.空的return
    return
}

Since defer closure is referenced, the return value is modified, so f () returns 1.

Example 2:
func f() (r int) {
    t := 5
    defer func() {
        t = t + 5
    }()
    return t
}

Dismantling process:

func f() (r int) {
    t := 5
    // 1.赋值
    r = t

    // 2.闭包引用,但是没有修改返回值r
    defer func() {
        t = t + 5
    }()

    // 3.空的return
    return
}

Since the return value r Step 2 of operation is not directed, 5 is returned.

Example 3:
func f() (r int) {
    defer func(r int) {
        r = r + 5
    }(r)
    return 1
}

Dismantling process:

func f() (r int) {

    // 1.赋值
    r = 1

    // 2.r作为函数参数,不会修改要返回的那个r值
    defer func(r int) {
        r = r + 5
    }(r)

    // 3.空的return
    return
}

Since the second step as a function of the parameter r is used, only one copy, the defer statement inside and the outside of r r actually two variables, the variables r changes which do not affect the outer variables r, so it is not returned 6, but returns 1.


Homepage:

www.codeapes.cn

Guess you like

Origin www.cnblogs.com/codeapes666/p/12093811.html