慕码人2483693
例如:package mainimport "fmt"type Attribute struct { Key, Val string}type Node struct { Attr []*Attribute}func main() { n := Node{[]*Attribute{ &Attribute{"foo", ""}, &Attribute{"href", ""}, &Attribute{"bar", ""}, }} for _, attr := range n.Attr { if attr.Key == "href" { attr.Val = "something" } } for _, v := range n.Attr { fmt.Printf("%#v\n", *v) }}操场输出量main.Attribute{Key:"foo", Val:""}main.Attribute{Key:"href", Val:"something"}main.Attribute{Key:"bar", Val:""}替代方法:package mainimport "fmt"type Attribute struct { Key, Val string}type Node struct { Attr []Attribute}func main() { n := Node{[]Attribute{ {"foo", ""}, {"href", ""}, {"bar", ""}, }} for i := range n.Attr { attr := &n.Attr[i] if attr.Key == "href" { attr.Val = "something" } } for _, v := range n.Attr { fmt.Printf("%#v\n", v) }}操场输出:main.Attribute{Key:"foo", Val:""}main.Attribute{Key:"href", Val:"something"}main.Attribute{Key:"bar", Val:""}