我编写了这段代码来制作切片副本,它工作正常,作为参数传递给函数的主切片不受影响:
package main
import "fmt"
type Team []Person
type Person struct {
Name string
Age int
}
func main() {
team := Team{
Person{"Hasan", 34}, Person{"Karam", 32},
}
fmt.Printf("original before clonning: %v\n", team)
team_cloned := team.Clone()
fmt.Printf("original after clonning: %v\n", team)
fmt.Printf("clones slice: %v\n", team_cloned)
}
func (c *Team) Clone() Team {
var s = make(Team, len(*c))
copy(s, *c)
for index, _ := range s {
s[index].Name = "change name"
}
return s
}
但是在我的其他代码中,即使我将原始切片传递clone给函数,原始切片也会发生变化:
type Inventories []Inventory
type Inventory struct { //instead of: map[string]map[string]Pairs
Warehouse string
Item string
Batches Lots
}
type Lots []Lot
type Lot struct {
Date time.Time
Key string
Value float64
}
func (c *Inventories) Clone() Inventories {
var s = make(Inventories, len(*c))
copy(s, *c)
return s
}
func (outs Inventories) BuildBatchesFrom(ins Inventories) (batchesBalance Inventories, outgoing Inventories) {
batchesOut := Inventories{}
for _, in := range batchesBalance {
for _, out := range outgoing {
if out.Warehouse == in.Warehouse && out.Item == in.Item {
batches := Lots{}
OUTER:
for {
oldestBatch := in.Batches.First()
batchQty := math.Min(in.Batches.First().Value, math.Abs(out.Batches.First().Value))
batches = append(batches, Lot{out.Batches.First().Date, oldestBatch.Key, batchQty})
out.Batches[0].Value = out.Batches.First().Value + batchQty
in.Batches[0].Value = oldestBatch.Value - batchQty
if in.Batches.First().Value == 0 {
in.Batches.PopFirst()
}
在上面,ins运行函数后正在发生变化,尽管我没有将它传递到那里,并且ins_clone()在函数之后发生变化,尽管我在函数代码的第一行克隆了它,对于outs和outs_clone()
梵蒂冈之花
相关分类