1 回答
TA贡献1900条经验 获得超5个赞
我明白了,原因是我的切片包含一个sub slice所以copy对顶部有用但不适用于共享相同数据的子切片,我通过创建一个新切片重写代码来修复它,推送数据到它,然后用它来替换原来的数据,如下所示:
func (i *Inventories) CloneFrom(c Inventories) {
inv := new(Inventories)
for _, v := range c {
batches := Lots{}
for _, b := range v.Batches {
batches = append(batches, Lot{
Date: b.Date,
Key: b.Key,
Value: b.Value,
})
}
*inv = append(*inv, Inventory{
Warehouse: v.Warehouse,
Item: v.Item,
Batches: batches,
})
}
(*i).ReplaceBy(inv)
}
func (i *Inventories) ReplaceBy(x *Inventories) {
*i = *x
}
- 1 回答
- 0 关注
- 100 浏览
添加回答
举报