首页 > 文章列表 > go defer 函数不能正确处理传递的指针

go defer 函数不能正确处理传递的指针

442 2024-03-26
问题内容

在我的代码中,我尝试使用 numaddr 来记录 defer 语句后 num 的变化

func deferrun() {
    num := 1
    numaddr := &num
    defer fmt.printf("num is %d", *numaddr)
    num = 2
    return
}

func main() {
    deferrun()
}

但我得到 num 是 1 而不是 2,为什么 defer 函数使用 *numaddr 的值而不是地址?

那我试试另一种方法

func deferRun() {
    num := 1
    numAddr := &num
    defer func(intAddr *int){
        fmt.Printf("num is %d", *numAddr)
    }(numAddr)
    
    num = 2
    fmt.Println("num is", *numAddr)
    return
}

func main() {
    deferRun()
}

这次它起作用了,我得到 num 是 2,所以我想也许 defer fmt.printf(something) 在声明它时立即存储了字符串,并且当 defer 函数实际运行时没有使用 numaddr ?


正确答案


有趣的问题。要回答这个问题,你必须知道一个规则,就像这个 go 教程 https://go.dev/游览/流量控制/12

延迟调用的参数会立即计算,但直到周围函数返回时才会执行函数调用。

示例 1:告诉 defer 函数打印位于指定内存地址的值。

func deferrun() {
    num := 1
    numaddr := &num //address of variable num in stack memory, 0xc000076f38 for example
    defer func(intaddr *int){
        fmt.printf("num is %d", *numaddr)
    }(numaddr) //hey go, when the surrounding function returns, print the value located in this address (numaddr=0xc000076f38)
    num = 2 //now the value located in address 0xc000076f38 is 2
    return  
}

输出将为 2。

示例 2:告诉 defer 函数打印指定的值。

func deferRun() {
    num := 1
    numAddr := &num //address of variable num in stack memory, 0xc000076f38 for example
    defer fmt.Printf("num is %d", *numAddr) //Hey Go, when the surrounding function returns, print the this value *numAddr (*numAddr is 1 for now)
    num = 2 //Now the value located in address 0xc000076f38 is 2 but you told the defer function to print 1 before
    return  
}

输出将为 1。