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 關注
- 119 瀏覽
添加回答
舉報