示例1
func Test1() {
str := "defer1"
defer fmt.Println(str)
str = "defer2"
defer func() {
fmt.Println(str)
}()
str = "defer3"
defer func(str string) {
fmt.Println(str)
}(str)
str = "defer4"
}
示例2
func Test2() {
str := []int{1}
defer fmt.Println(str)
str[0] = 2
defer func() {
fmt.Println(str)
}()
str[0] = 3
defer func(str []int) {
fmt.Println(str)
}(str)
str[0] = 4
}
示例3
func Test3() {
str := []int{1}
defer fmt.Println(str)
str = []int{2}
defer func() {
fmt.Println(str)
}()
str = []int{3}
defer func(str []int) {
fmt.Println(str)
}(str)
str = []int{4}
}
三个示例各有千秋,懂了就真的懂了
网友评论