Maps vs Struct

blov · · 739 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<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&#39;t all the same type as a map would require them to be.</p></pre>RSoreil: <pre><p>I don&#39;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&#39;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&#39;s not true if you have your keys and values stored as structs in an array. </p></pre>

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

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