“演示匿名函数可分配不同的内存地址” 这个例子实际运行结果不同

for i := 0; i < 4; i++ {\
   g := func(i int) { fmt.Printf("%d ", i) } //此例子中只是为了演示匿名函数可分配不同的内存地址,在现实开发中,不应该把该部分信息放置到循环中。\
  g(i)\
   fmt.Printf(" - g is of type %T and has value %v\n", g, g)\
}

教程给出的结果是四个不同的内存地址。

我自己本地跑出来的内存地址是一样的。

0  - g is of type func(int) and has value 0x611c90
1  - g is of type func(int) and has value 0x611c90
2  - g is of type func(int) and has value 0x611c90
3  - g is of type func(int) and has value 0x611c90

go环境:


go version go1.12.5 windows/amd64
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!