首页  >  文章  >  后端开发  >  对于同一结构体,Golang 在 for 循环中 defer 的行为有所不同

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

WBOY
WBOY转载
2024-02-06 09:30:11935浏览

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

问题内容

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

type test struct {
    name string
}

func (t test) hello() {
    fmt.printf("hello, %s\n", t.name)
}

func (t *test) hello2() {
    fmt.printf("pointer: %s\n", 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 在 for 循环中 defer 的行为有所不同的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:stackoverflow.com。如有侵权,请联系admin@php.cn删除