dshakcsq64956 2018-10-09 09:53
浏览 690
已采纳

golang defer语句是在return语句之前还是之后执行?

I have a question about golang defer: Is golang defer statement execute before or after return statement?

I have read Defer_statements. But I do not got the answer.

I made a simple test:

func test1() (x int) {
    defer fmt.Printf("in defer: x = %d
", x)

    x = 7
    return 9
}

func test2() (x int) {
    defer func() {
        fmt.Printf("in defer: x = %d
", x)
    }()

    x = 7
    return 9
}

func test3() (x int) {
    x = 7
    defer fmt.Printf("in defer: x = %d
", x)
    return 9
}

func main() {
    fmt.Println("test1")
    fmt.Printf("in main: x = %d
", test1())
    fmt.Println("test2")
    fmt.Printf("in main: x = %d
", test2())
    fmt.Println("test3")
    fmt.Printf("in main: x = %d
", test3())
}

In test1(), using Printfto print x after defer. In test2(), using a anonymous function to print x after defer. In test3(), using Printfto print x after defer, but defer after x = 7.

But the result is:

test1
in defer: x = 0
in main: x = 9
test2
in defer: x = 9
in main: x = 9
test3
in defer: x = 7
in main: x = 9

So, is any one can explain: 1. why got this result? why test1 prints 0, test2 print9, test3 prints 7. 2. is defer statement excutes after return or before return?

Thanks a lot.

  • 写回答

4条回答 默认 最新

  • douxianwu2221 2018-11-09 04:37
    关注

    Thanks @dev.bmax @Tranvu Xuannhat @rb16. With your help, I found the key explanation from Defer_statements.

    Each time a "defer" statement executes, the function value and parameters to the call are evaluated as usual and saved anew but the actual function is not invoked.

    We can break up defer ... into three parts.

    1. invoke defer, evaluating the value of function parameter.
    2. execute defer, pushing a function in to stack.
    3. execute functions in stack after return or panic.

    I made a new test4 to explain.

    func test4() (x int) {
        defer func(n int) {
            fmt.Printf("in defer x as parameter: x = %d
    ", n)
            fmt.Printf("in defer x after return: x = %d
    ", x)
        }(x)
    
        x = 7
        return 9
    }
    

    In test4,

    1. invoke defer, evaluating the value of n, n = x = 0, so x as parameter is 0.
    2. execute defer, pushing func(n int)(0) onto stack.
    3. execute func(n int)(0) after return 9, n in fmt.Printf("in defer x as parameter: x = %d ", n) has been evaluated, x in fmt.Printf("in defer x after return: x = %d ", x) will be evaluated now, x is 9。

    So, got the result:

    test4
    in defer x as parameter: x = 0
    in defer x after return: x = 9
    in main: x = 9
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥100 Acess连接SQL 数据库后 不能用中文筛选
  • ¥20 电脑拓展屏桌面被莫名遮挡
  • ¥20 ensp,用局域网解决
  • ¥15 Python语言实验
  • ¥15 我每周要在投影仪优酷上自动连续播放112场电影,我每一周遥控操作一次投影仪,并使得电影永远不重复播放,请问怎样操作好呢?有那么多电影看吗?
  • ¥20 电脑重启停留在grub界面,引导出错需修复
  • ¥15 matlab透明图叠加
  • ¥50 基于stm32l4系列 使用blunrg-ms的ble gatt 创建 hid 服务失败
  • ¥150 计算DC/DC变换器平均模型中的参数mu
  • ¥25 C语言代码,大家帮帮我