内容简介:下面举例说明了defer语句的作用:输出结果:很明显 defer语句的执行顺序是反的。这种语法给我们的资源关闭提供了很好的帮助。
defer
下面举例说明了defer语句的作用:
package main
import "fmt"
func main() {
i := 10
defer incr(i)
defer incr2(i)
defer incr3(i)
}
func incr(i int){
j := i -1
fmt.Println(j)
}
func incr2(i int){
j := i -2
fmt.Println(j)
}
func incr3(i int){
j := i -3
fmt.Println(j)
}
输出结果:
GOROOT=C:\Go #gosetup GOPATH=C:\Users\DELL\go #gosetup C:\Go\bin\go.exe build -o C:\Users\DELL\AppData\Local\Temp\___go_build_cmpl_go.exe C:/Users/DELL/ActiveGo/src/spider/main/cmpl.go #gosetup "C:\Program Files\JetBrains\GoLand 2018.3.2\bin\runnerw64.exe" C:\Users\DELL\AppData\Local\Temp\___go_build_cmpl_go.exe #gosetup 7 8 9 Process finished with exit code 0
很明显 defer语句的执行顺序是反的。这种语法给我们的资源关闭提供了很好的帮助。
比如流的关闭啊,文件的关闭啊等等。
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Distributed Algorithms: An Intuitive Approach
Wan Fokkink / MIT Press / 2018-2-2 / USD 48.00
The new edition of a guide to distributed algorithms that emphasizes examples and exercises rather than the intricacies of mathematical models. This book offers students and researchers a guide to ......一起来看看 《Distributed Algorithms: An Intuitive Approach》 这本书的介绍吧!