golang的defer
#############################
package main import ( "fmt" "time" ) // 规则一 当defer被声明时,其参数就会被实时解析 // 规则二 多个defer执行顺序:defer执行顺序为先进后出 // 规则三 defer可以读取有名返回值 // 规则四 执行时机:函数return之后,函数返回给调用方之前之前 func DeferTimeX() { startAt := time.Now() defer fmt.Println(time.Since(startAt)) time.Sleep(10 * time.Second) } func DeferTimeY() { startAt := time.Now() defer func() { fmt.Println(time.Since(startAt)) }() time.Sleep(3 * time.Second) } func DeferOrder() { defer fmt.Println("defer runs") fmt.Println("block ends") for i := 0; i < 10; i++ { defer fmt.Println(i) } fmt.Println("main ends") } func DeferPara() { i := 2 defer fmt.Println("************* ", i) //输出2,因为i此时就是2 i = i * 2 defer fmt.Println("$$$$$$$$$$$$$ ", i) //输出4,因为i此时就是4 i = i * 2 fmt.Println("=================== ", i) //输出8 } // 没有修改返回值 func DeferFuncX() int { var i int fmt.Println("DeferFuncX ..........") fmt.Println("init at :", i) defer func() { fmt.Println("starting at :", i) //i此时就是16 i++ fmt.Println("end at :", i) //输出17 }() return 16 } // 修改了返回值 func DeferFuncY() (i int) { fmt.Println("DeferFuncY ..........") fmt.Println("init at :", i) defer func() { fmt.Println("starting at :", i) //i此时就是16 i++ fmt.Println("end at :", i) //输出17 }() return 16 } func main() { DeferTimeX() DeferTimeY() DeferOrder() DeferPara() fmt.Println(DeferFuncX()) fmt.Println(DeferFuncY()) }
##########################
igoodful@qq.com