gpt4 book ai didi

go - 在复制 slice 并使用副本后更改 slice

转载 作者:行者123 更新时间:2023-12-01 22:44:19 27 4
gpt4 key购买 nike

我编写了以下代码以进行 slice 复制,并且工作正常,并且不影响作为参数传递给函数的主 slice :

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从它传递给函数,原始 slice 也会发生变化:

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()
}
if out.Batches.First().Value == 0 {
out.Batches.PopFirst()
if len(out.Batches) == 0 {
break
} else {
continue OUTER
}
} else {
continue OUTER
}
}
batchesOut = append(batchesOut, Inventory{
Warehouse: out.Warehouse,
Item: out.Item,
Batches: batches,
})
}
}
//os.Exit(3)
}
return batchesOut, batchesBalance
}

func main() {
ins := Inventory{
Warehouse: "DMM",
Item: "Gloves",
Batches: Lots{
Lot{mustTime(time.Parse(custom, "1/7/2020")), "Jan", 50},
Lot{mustTime(time.Parse(custom, "2/1/2020")), "Feb", 70},
},
}

outs := Inventory{
Warehouse: "DMM",
Item: "Gloves",
Batches: Lots{
Lot{mustTime(time.Parse(custom, "1/5/2020")), "", -10},
Lot{mustTime(time.Parse(custom, "2/9/2020")), "", -30},
},
}

fmt.Printf("\n\n[1] Ins: \n%v", ins) // This output is different after running outs_clone.BuildBatchesFrom(ins_clone)
fmt.Printf("\n\n[2] Outs: \n%v", outs) // // This output is different after running outs_clone.BuildBatchesFrom(ins_clone)

ins_clone := ins.Clone()
outs_clone := outs.Clone()

batchesOut, batchesBalance := outs_clone.BuildBatchesFrom(ins_clone)

fmt.Printf("\n\n[1] Ins: \n%v", ins) // This output is different before running outs_clone.BuildBatchesFrom(ins_clone)
fmt.Printf("\n\n[2] Outs: \n%v", outs) // This output is different after running outs_clone.BuildBatchesFrom(ins_clone)

fmt.Printf("\n\n[4] Batches outs: \n%v", batchesOut)
fmt.Printf("\n\n[5] Batches Balances: \n%v", batchesBalance)
}

在上面的代码中, ins在运行函数后发生了变化,尽管我没有在那儿传递它,并且 ins_clone()在函数后发生了变化,尽管我在函数代码的第一行中对其进行了克隆, outsouts_clone()相同

最佳答案

我明白了,原因是我的 slice 包含一个sub slice,所以copy适用于顶部的一个,但不适用于共享相同数据的下一个,我通过创建一个新的 slice 来重写代码来修复它,将数据推送到其中,然后使用它替换原来的数据,如下所示:

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
}

关于go - 在复制 slice 并使用副本后更改 slice ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62222991/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com