切片在复制并使用副本后发生变化

我编写了这段代码来制作切片副本,它工作正常,作为参数传递给函数的主切片不受影响:


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()


千万里不及你
浏览 101回答 1
1回答

梵蒂冈之花

我明白了,原因是我的切片包含一个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}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go