defer里面的值何时evaluate

栏目: Go · 发布时间: 6年前

内容简介:我在写这个的时候只是想记录一下像标题中说的, defer里面的值何时evaluate, 就像下面这一个例子.例子来自于结果是这样的

第一个问题

我在写这个的时候只是想记录一下像标题中说的, defer里面的值何时evaluate, 就像下面这一个例子.

例子来自于 https://groups.google.com/forum/#!topic/golang-nuts/c7YUK65Xqgs%5B1-25%5D

package main
import (
    "fmt"
)
func main() {
    for i := 0; i < 10; i++ {
        defer func() {
            fmt.Printf("func(): %v\n", i)
        }()
        defer printi(i)
        defer fmt.Printf("i: %v\t", i)
    }
}
func printi(i int) {
    fmt.Printf("printi(): %v\t", i)
}

结果是这样的

OUTPUT
======
i: 9	printi(): 9	func(): 10

i: 8	printi(): 8	func(): 10

i: 7	printi(): 7	func(): 10

i: 6	printi(): 6	func(): 10

i: 5	printi(): 5	func(): 10

i: 4	printi(): 4	func(): 10

i: 3	printi(): 3	func(): 10

i: 2	printi(): 2	func(): 10

i: 1	printi(): 1	func(): 10

i: 0	printi(): 0	func(): 10

有趣的是, 匿名函数里面的输出和Print(也就是非匿名函数)里面的输出是不一样的. 这里可以用一个原因来解决(有种大统一理论的感觉不… )

二楼的 emepyc 说的很清楚, 我也不画蛇添足了, 直接复制过来

The key point is that the arguments to the deferred functions are 
evaluated and copied in the invocation of the defer. So: 

 >          defer  func()  { 
 >              fmt.Printf("func():  %v\n",  i) 
 >          }() 

No arguments to func(), so i is not copied and the final value of i is 
used when the deferred function is executed. 

 >          defer  printi(i) 
 >          defer  fmt.Printf("i:  %v\t",  i) 

In both cases, i is the argument of the deferred function, so its value 
is copied and used when the deferred function is evaluated.

第二个问题

第二个问题, 在我看完之后, 并看了相关的链接资料, 发现是第一个问题的超集.

先来看一下原问题, https://stackoverflow.com/questions/51360229/the-deferred-calls-arguments-are-evaluated-immediately

这个回答里面也给出了 golang 官方资料关于 defer 的链接, 我之前在 google 搜索 golang defer 在第一页居然没有出现这个官方链接 .. https://golang.org/ref/spec#Defer_statements . 官方资料里面言简意赅的解释了 defer 的执行规则, 但是… 如果不能精确理解其中一些术语的话, 会看的很不明白, 比如我..

由三句话组成, 分别对应三个执行规则.

  1. 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.
  2. Instead, deferred functions are invoked immediately before the surrounding function returns, in the reverse order they were deferred. That is, if the surrounding function returns through an explicit return statement, deferred functions are executed after any result parameters are set by that return statement but before the function returns to its caller.
  3. If a deferred function value evaluates to nil, execution panics when the function is invoked, not when the “defer” statement is executed.

写 Blog 真的还挺累(麻烦)的, 具体的意思我就不再解释了. https://stackoverflow.com/questions/51360229/the-deferred-calls-arguments-are-evaluated-immediately 这里对第一点, 第二点, 都有很好的解释了.


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Spark SQL内核剖析

Spark SQL内核剖析

朱锋、张韶全、黄明 / 电子工业出版社 / 2018-8 / 69.00元

Spark SQL 是 Spark 技术体系中较有影响力的应用(Killer application),也是 SQL-on-Hadoop 解决方案 中举足轻重的产品。《Spark SQL内核剖析》由 11 章构成,从源码层面深入介绍 Spark SQL 内部实现机制,以及在实际业务场 景中的开发实践,其中包括 SQL 编译实现、逻辑计划的生成与优化、物理计划的生成与优化、Aggregation 算......一起来看看 《Spark SQL内核剖析》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具