卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章34196本站已运行391

对于同一结构体,Golang 在 for 循环中 defer 的行为有所不同

对于同一结构体,golang 在 for 循环中 defer 的行为有所不同

问题内容

我编写了以下 golang 代码并运行了它。

type test struct {
    name string
}

func (t test) hello() {
    fmt.printf("hello, %sn", t.name)
}

func (t *test) hello2() {
    fmt.printf("pointer: %sn", t.name)
}

func runt(t test) {
    t.hello()
}

func main() {
    mapt := []test{
        {name: "a"},
        {name: "b"},
        {name: "c"},
    }

    for _, t := range mapt {
        defer t.hello()
        defer t.hello2()
    }
}

输出:

pointer: C
Hello, C
pointer: C
Hello, B
pointer: C
Hello, A

我的理解是,指针 t 在 3 个循环后将指向“c”,因此三个 3“c”用于“hello2”输出。然而,延迟“hello”函数调用的行为非常奇怪。看起来它正在保留它所指向的位置。 (t test) 如何影响这个?

我很好奇golang将其编译成什么。非常感谢!


正确答案


在 for 循环内,defer 语句的参数是一个闭包。闭包捕获循环变量 t

对于使用值接收器的调用,闭包包含 t 的副本。对于使用指针接收器的调用,闭包包含一个指向 t 的指针。

循环变量在每次迭代时都会被重写(此行为将在该语言的更高版本中更改)。因此,值接收器闭包捕获每个值,而指针接收器闭包仅捕获指针,因此,在运行时,它们使用该指针的最新值。

卓越飞翔博客
上一篇: 如何在 Golang 中验证 JWE 令牌
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏