嗯…这是人们在Go中附加到切片时最常犯的错误。您必须将结果分配回slice。
func (box *MyBox) AddItem(item MyBoxItem) []MyBoxItem { box.Items = append(box.Items, item) return box.Items}另外,您已经定义
AddItem了
*MyBox类型,因此将该方法称为
box.AddItem(item1)

嗯…这是人们在Go中附加到切片时最常犯的错误。您必须将结果分配回slice。
func (box *MyBox) AddItem(item MyBoxItem) []MyBoxItem { box.Items = append(box.Items, item) return box.Items}另外,您已经定义
AddItem了
*MyBox类型,因此将该方法称为
box.AddItem(item1)