<p>Here's the scenario: I'm defining an type for a function that I want to pass into another function. This type is called Worker. Currently, a Worker function accepts a single argument that is a byte slice.</p>
<p>Now, as we know, thing change over time, so I'm thinking that if I create a new type that, for now, is a struct that just contains a byte slice. Now, this lets me add additional fields in the future without having to change the implementation of functions. However, I don't know if this will happen, so I won't want to cause any stupid little issues by doing this. I don't think performance will be an issue or it will at least be minimal.</p>
<p>Any other potential gotchas that I'm not thinking of?</p>
<hr/>**评论:**<br/><br/>ammarbandukwala: <pre><p>If you're passing around this struct by value, there will be zero difference in performance from passing around just the <code>[]byte</code></p>
<p>Structs contain no special data in addition to their members when represented in memory, so the wrapper struct with a single <code>[]byte</code> is for performance sake equivalent to a pure <code>[]byte</code></p></pre>sybrandy: <pre><p>Thanks. That's what I thought. I just had one of those nagging feelings that I may be missing something.</p></pre>adonovan76: <pre><p>Even if you don't add fields in future, the struct may serve as an encapsulation mechanism because it hides the representation of its unexported fields from clients' prying eyes.</p></pre>sybrandy: <pre><p>Understood and fully agree. In this case, it's just a message being passed between goroutines, so I'm not concerned about that at the moment.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传