A map of arrays question

blov · 2017-10-24 16:00:08 · 637 次点击    
这是一个分享于 2017-10-24 16:00:08 的资源,其中的信息可能已经有所发展或是发生改变。

So, I am confused. I am coming from a Python background and trying to learn Go. So, in Python I can do:

age_dict = {}
age_dict[40] = []
age_dict[40].append(MyData(someone)
age_dict[40].append(MyData(someonelse))
age_dict[2] = [MyData(kid)]

This would produce:

{2: [<MyData object at 0x7fea79de96d0>],
 40: [<MyData object at 0x7fea79de9ed0>, <MyData object at 0x7fea79de9e10>]}

This would give me a dictionary (read: map) which has an integer as a key and has a list (read:array) of values. I would like to do something similar in Go, but I am just not getting it.

type MyData struct {
  name string
  age int8
}
age_map = map[int][]MyData
age_map[40].append(age_map[40], MyData{"someone", 40})
age_map[40].append(age_map[40], MyData{"someone", 40})

So, what am I doing wrong? I am just not understanding how to do this - if it's possible at all. My real issue is understanding the whole:

   map[int][]MyCustomType

Syntax. Can someone point me in the correct direction?


评论:

anaerobic_lifeform:

The new slice is given by the return value of "append".

https://blog.golang.org/slices

Kraigius:

To expand and to help avoid a common pitfall when starting go, a slice is basically a view on an array. 98% of the time when appending, you will want to assign the result back to the very same slice, this isn't a new variable. The view changed but it's using the same array in the back.

When you append you add something at s[len(s)] of the view. If you start to assign the result of an append to a different slice you will end up overwriting values.

Demonstration: https://play.golang.org/p/xlQZs7uyNf

This might seems like I'm throwing random knowledge that isn't related to OP problem at hand but it's such a common pitfall when starting using slice and it isn't well communicated that I just want to share it.

TheMerovius:

Does this help a bit?

TheMerovius:

to explain:

  • map[int][]MyData is a type, read it as: "map from int to slice of ([]) MyData"
  • make(T) will create a new value of type T - in this case, above map. So it's equivalent to the {} from the first line of your python code, just statically typed
  • age_map := … declares a new variable with name age_map and assigns to it. See the spec for details.
  • In Go, append is not a method on slices (which is the correct equivalent of a python list; in Go, an array has a fixed length and is something different). Instead, there is a builtin function append, which takes a slice and some elements and returns the slice with the elements appended. So, it doesn't work in-place and you need to assign the return value to use it.
petezhut:

This did it. I was confused by the append syntax. Many, many thanks.


入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

637 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传