Any consequences to wrapping simple data types in a struct?

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

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.

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.

Any other potential gotchas that I'm not thinking of?


评论:

ammarbandukwala:

If you're passing around this struct by value, there will be zero difference in performance from passing around just the []byte

Structs contain no special data in addition to their members when represented in memory, so the wrapper struct with a single []byte is for performance sake equivalent to a pure []byte

sybrandy:

Thanks. That's what I thought. I just had one of those nagging feelings that I may be missing something.

adonovan76:

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.

sybrandy:

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.


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

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