1、多个defer顺序
package main
import "fmt"
func main() {
//defer多个,打印顺序是倒的,类似出栈
defer fmt.Println("end1")
defer fmt.Println("end2")
fmt.Println("aaa")
fmt.Println("bbb")
}
2、defer与return 的执行顺序
package main
import "fmt"
func main() {
deferAndReturn()
}
func deferAndReturn() int {
defer deferFunc()
return returnFunc()
}
func deferFunc() int {
fmt.Println("defer func called...")
return 0
}
func returnFunc() int {
fmt.Println("returnFunc func called...")
return 0
}
结果:return比defer先执行