读入一个结构会覆盖另一个结构

问题描述 投票:-1回答:1

我在Go中管理结构有一些问题。我有复杂的结构和两个基于该结构的变量 - “previous”和“current”。我正在尝试从tarfile中读取数据,进行一些计算并用current替换之前的数据。但是在我读到当前的下一次阅读迭代时,在我看来,“先前”的内容被覆盖,两个变量都变得相同。结构定义如下:

type Mystruct struct {
    Data       [][]sql.NullString
    Rnames     []string
    Nsize      int
    Msize      int
    Namemaxlen map[string]int
    Valid      bool
    Err        error
}

变量不是指针。复制作为直接赋值执行:previous = current。

tr := tar.NewReader(f)
var prev, curr Mystruct

for {
    hdr, err := tr.Next()
    if err == io.EOF {
        break
    } else if err != nil {
        log.Panicln(err)
    }

    data := make([]byte, hdr.Size)
    if _, err := io.ReadFull(tr, data); err != nil {
        log.Panicln(err)
    }

    if err = json.Unmarshal(data, &curr); err != nil {
        log.Panicln(err)
    }

    if prev.Valid != true {
        prev = curr
        continue
    }

    // other computations here

    prev = curr
}

我哪里错了?提前致谢。

go struct
1个回答
0
投票

问题是你的struct包含基本上是指向内存的指针。复制这些指针意味着您的副本指向SAME内存作为原始内容,因此它们共享切片值。改变一个会改变另一个。

这是a small example来说明问题:

package main

import "fmt"

type s struct {
    a     int
    slice []int
}

func main() {
    // create the original thing
    prev := s{
        a:     5,
        slice: []int{1, 2, 3},
    }
    // copy the thing into cur
    cur := prev
    // now change cur, changing a will change only cur.a because integers are
    // really copied
    cur.a = 6
    // changing the copied slice will actually change the original as well 
    // because copying a slice basically copies the pointer to memory and the
    // copy points to the same underlying memory area as the original
    cur.slice[0] = 999
    // printing both, we can see that the int a was changed only in the copy but
    // the slice has changed in both variables, because it references the same
    // memory
    fmt.Println(prev)
    fmt.Println(cur)
}
© www.soinside.com 2019 - 2024. All rights reserved.