defer是
LIFO或堆栈-
保证以相反的顺序执行。它获取第一个
defer,并将其放在某个内部堆栈中(可能我不知道血腥的细节),然后将下一个放在该堆栈的
defer顶部,然后当它到达函数末尾时,展开,开始在顶部。它看起来像是在
for-loop里做的(我知道这是Go的示例,而不是您的示例),但是在其他情况下,一个功能依赖于其他功能的清理,这更有意义,为什么它应该是IS,保证执行顺序相反。
这是一个不同的示例,都是伪代码,但希望这一点很清楚。
open stream1defer close stream1defer write stream1 "stream2 better be closed, or we are in trouble..."open stream2defer close stream2defer stream2 "this is the last you'll ever hear from stream2"connect stream2 to stream1write stream2 "hey, we are in stream2, this feeds into stream1"
应该打印类似:
"hey, we are in stream2, this feeds into stream1""this is the last you'll ever hear from stream2""stream2 better be closed, or we are in trouble..."
如果您没有关于反向订购的保证,则不能确定
stream1您在期间仍处于打开状态
defer stream2 write。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)