<p>So, I am confused. I am coming from a Python background and trying to learn Go. So, in Python I can do:</p>
<pre><code>age_dict = {}
age_dict[40] = []
age_dict[40].append(MyData(someone)
age_dict[40].append(MyData(someonelse))
age_dict[2] = [MyData(kid)]
</code></pre>
<p>This would produce:</p>
<pre><code>{2: [<MyData object at 0x7fea79de96d0>],
40: [<MyData object at 0x7fea79de9ed0>, <MyData object at 0x7fea79de9e10>]}
</code></pre>
<p>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. </p>
<pre><code>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})
</code></pre>
<p>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:</p>
<pre><code> map[int][]MyCustomType
</code></pre>
<p>Syntax. Can someone point me in the correct direction?</p>
<hr/>**评论:**<br/><br/>anaerobic_lifeform: <pre><p>The new slice is given by the return value of "append".</p>
<p><a href="https://blog.golang.org/slices" rel="nofollow">https://blog.golang.org/slices</a></p></pre>Kraigius: <pre><p>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.</p>
<p>When you append you add something at <code>s[len(s)]</code> of the view. If you start to assign the result of an append to a different slice you will end up overwriting values.</p>
<p>Demonstration: <a href="https://play.golang.org/p/xlQZs7uyNf" rel="nofollow">https://play.golang.org/p/xlQZs7uyNf</a></p>
<p>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.</p></pre>TheMerovius: <pre><p>Does <a href="https://play.golang.org/p/ii7aB7uWMA" rel="nofollow">this</a> help a bit?</p></pre>TheMerovius: <pre><p>to explain:</p>
<ul>
<li><code>map[int][]MyData</code> is a type, read it as: "<code>map</code> from <code>int</code> to slice of (<code>[]</code>) <code>MyData</code>"</li>
<li><code>make(T)</code> will create a new value of type <code>T</code> - in this case, above map. So it's equivalent to the <code>{}</code> from the first line of your python code, just statically typed</li>
<li><code>age_map := …</code> declares a new variable with name <code>age_map</code> and assigns <code>…</code> to it. See <a href="https://golang.org/ref/spec#Short_variable_declarations" rel="nofollow">the spec</a> for details.</li>
<li>In Go, <code>append</code> 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 <code>append</code>, 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.</li>
</ul></pre>petezhut: <pre><p>This did it. I was confused by the append syntax. Many, many thanks.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
0 回复
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传