defer的陷阱
func main() {
fmt.Println("Before if")
if true {
defer fmt.Println("inside if")
}
fmt.Println("Ater if")
}
// ----Output-----
// Before if
// Ater if
// inside if在延迟函数中使用外部变量
func main() {
for i := 0; i < 2; i++ {
defer func() {
fmt.Printfln("%d", i)
}()
}
}
// ----Output----
// 2
// 2最后更新于