简体   繁体   English

go语言中如何将带有map的结构体存储到数组中

[英]How to store struct with map into an Array in the go language

I made a package that looks like this...我做了一个看起来像这样的包......

package foo
type Foo struct {
    num   int
    aMap  map[int](int)
}

func MakeFoo() BookState {
    return Foo{
        num:  -1,
        aMap: make(map[int](int)),
    }
}

I'm processing rows of a file like this我正在处理这样的文件行

nrows :=100
arrayFoo = make([]Foo, nrows)
Foo = foo.MakeFoo()
count := 0
for int i=0; i < nrows; i++ {
   row = myWrappedReader.ReadLine()
   foo.num = i
   foo.aMap[key] += row.otherNum
   arrayFoo[i] = foo
}

But then when I go to check the arrayFoo at the end I have something that looks like this [{num:1, aMap:{/*final state*/}, {num:2, aMap:{/*final state*/}, ...]但是当我最后去检查arrayFoo时,我有一些看起来像这样的东西[{num:1, aMap:{/*final state*/}, {num:2, aMap:{/*final state*/}, ...]

So the integer is updating but I need a copy of aMap to be stored instead of just the pointer to aMap .所以整数正在更新,但我需要存储aMap的副本,而不仅仅是指向aMap的指针。


Update:更新:

Here's a playground .这里是游乐场

Update2:更新2:

Here's a version that works .这是一个有效的版本 My class is quite a bit more complicated than this so I think I'll write a helper function in package foo that clone it.我的课程比这复杂得多,所以我想我会在package foo中编写一个辅助函数来克隆它。

Is there a easier way to copy maps or do most people do that?有没有更简单的方法来复制地图,或者大多数人都这样做?

Anything requiring a deep copy, as mentioned in " Is there a built in function in go for making copies of arbitrary maps? ", would involve a dedicated function.任何需要深度复制的东西,如“ go 中是否有用于制作任意地图副本的内置函数? ”中所述,都将涉及专用函数。

Example in this gist : 本要点中的示例:

package deepcopy

import (
    "bytes"
    "encoding/gob"
)

func init() {
    gob.Register(map[string]interface{}{})
}

// Map performs a deep copy of the given map m.
func Map(m map[string]interface{}) (map[string]interface{}, error) {
    var buf bytes.Buffer
    enc := gob.NewEncoder(&buf)
    dec := gob.NewDecoder(&buf)
    err := enc.Encode(m)
    if err != nil {
        return nil, err
    }
    var copy map[string]interface{}
    err = dec.Decode(&copy)
    if err != nil {
        return nil, err
    }
    return copy, nil
}

Based on the suggestion of mkopriva replacing the line foo.aMap[key] += i with foo.aMap = map[string]int{"key": foo.aMap[key] + i}根据 mkopriva 的建议,将foo.aMap[key] += i行替换为foo.aMap = map[string]int{"key": foo.aMap[key] + i}

package main

import (
    "fmt"
)

type Foo struct {
    num  int
    aMap map[string](int)
}

func MakeFoo() Foo {
    return Foo{
        num:  -1,
        aMap: make(map[string](int)),
    }
}

func main() {
    foo := MakeFoo()

    key := "tmp"
    foo.aMap[key] = 0

    fmt.Println(foo)
    arrayFoo := make([]Foo, 10)
    for i := 0; i < 10; i++ {
        foo.num = i
        foo.aMap = map[string]int{"key": foo.aMap[key] + i}
        arrayFoo[i] = foo
    }
    fmt.Println(arrayFoo)
}

Output:输出:

{-1 map[tmp:0]}
[{0 map[key:0]} {1 map[key:1]} {2 map[key:2]} {3 map[key:3]} {4 map[key:4]} {5 map[key:5]} {6 map[key:6]} {7 map[key:7]} {8 map[key:8]} {9 map[key:9]}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM