<p>Hi all<br/>
What is more expensive for memory datatype map or struct?</p>
<hr/>**评论:**<br/><br/>dgryski: <pre><p>A struct will always use less memory than a map storing the same keys/values. You also have more type safety with structs if your values aren't all the same type as a map would require them to be.</p></pre>RSoreil: <pre><p>I don't want to project too much of how C works on to Go but a struct is essentially nothing but a label for a set of datatypes stacked next to each other in memory. A quick look at the playground seems to comfirm this <a href="http://play.golang.org/p/Q9t-MSch6h">http://play.golang.org/p/Q9t-MSch6h</a></p>
<p>A map is a hashmap and will take a lot more memory to store the map.</p></pre>emadera52: <pre><p>There's no question that a map takes more memory. On the other hand, if your use case involves using a key to find a value (and the values are all of the same type), a map fills the bill without additional effort. That's not true if you have your keys and values stored as structs in an array. </p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
0 回复
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传