调用示例一:
// main.go
package main
import "fmt"
func main() {
fmt.Println("a")
defer fmt.Println("b")
defer fmt.Println("c")
}
调用示例二:
// main.go
package main
import "fmt"
func main() {
for i := 0; i < 3; i++ {
defer fmt.Println(i)
}
}
调用示例三:
// main.go 打印三个3
package main
import "fmt"
func main() {
for i := 0; i < 3; i++ {
defer func() {
fmt.Println(i)
}()
}
}
调用示例四(示例三闭包版):
// main.go
package main
import "fmt"
func main() {
for i := 0; i < 3; i++ {
defer func(a int) {
fmt.Println(a)
}(i)
}
}