【发布时间】:2016-02-10 04:10:38
【问题描述】:
为什么结果是A:&{[{[{1}]}]}A:&{[{[{2}]}]}A:&{[{[{2}]}]}
不是:A:&{[{[{1}]}]}A:&{[{[{2}]}]}A:&{[{[{3}]}]}
我们不能在范围内使用指针? 这是代码,我设置了一个指针,指向范围循环,但它失败了。
package main
import(
"fmt"
)
type A struct{
Barry []B
}
func (this *A)init(){
b:=&B{}
b.init()
this.Barry=[]B{*b}
return
}
type B struct{
Carry []C
}
func (this *B)init(){
c:=&C{}
c.init()
this.Carry=[]C{*c}
return
}
type C struct{
state string
}
func (this *C)init(){
this.state="1"
return
}
func main(){
a:=&A{}
a.init()
fmt.Printf("A:%v\n",a)
p:=&a.Barry[0].Carry[0]
p.state="2"
fmt.Printf("A:%v\n",a)
for _,v:=range a.Barry[0].Carry{
if v.state=="2"{
p=&v
}
}
p.state="3"
fmt.Printf("A:%v\n",a)
}
【问题讨论】: