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:
sybrandy: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
adonovan76:Thanks. That's what I thought. I just had one of those nagging feelings that I may be missing something.
sybrandy: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.
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.
