【发布时间】:2020-03-31 07:50:48
【问题描述】:
type A struct {
x1 []int
x2 []string
}
func (this *A) Test() {
fmt.Printf("this is: %p, %+v\n", this, *this)
}
func main() {
var fn func()
{
a := &A{}
a.x1 = []int{1, 2, 3}
a.x2 = []string{"one", "two", "three"}
fn = a.Test
}
fn()
}
请看:https://play.golang.org/p/YiwHG0b1hW-
我的问题是:
- 是否会在 {} 本地范围之外发布“a”?
- “a”的生命周期是否等于“fn”的生命周期?
【问题讨论】:
-
这是一种创建闭包的迂回方式。闭包在 Go 中运行良好。
-
此外,正如您所证明的,它在操场上工作的事实回答了您自己的问题。
标签: pointers go member-functions