Underneath there are optimisations to solve this issue.
http://research.swtch.com/2009/12/go-data-structures-interfaces.html
--
=====================
http://jessta.id.au
That's not right. When you pass an interface by value, only a copy of
the interface is made. The interface is a struct consisting of just
two machine words: a pointer to type information and a pointer to
data, which is actually just used for storage if the object itself is
no larger than a machine word. (So I guess technically, if the object
is no larger than a machine word, a copy is made, but that seems
reasonable to me.)
- Evan
Someone correct me if I'm wrong.When you pass an interface by value, a copy of the original object has to be made for the receiving formal parameter. So, if your object is large, you should expect large copies to happen, just like it would happen if you passed the original object by value.
This copy would only happens when you actually call the methods on
the interface.
--
=====================
http://jessta.id.au
I see the optimization here. The language doesn't have operations that allow you to directly access the value stored by the interface. So, you can maintain value semantics without having to copy.All you have to do is defer the copy until a type assertion. A method invocation falls in this category since there is an implied type assertion there.
No, but not in the way you think.
Calling a method with a value receiver necessitates copying that
value. If you defined your method on *Int instead of Int, it would
behave as you expect.
Andrew
On 18 February 2011 13:49, Steven <stev...@gmail.com> wrote:
> I'm asking if the current Go implementation is smart enough to only copy
> what it needs to make the function call. I'm not sure you've answered that
> question.
In this case, only the inner Int will be copied. It wouldn't make
sense to copy the outer T struct when invoking GetValue.
Your interpretation here is correct:
> The T struct won't fit into the receiver of the function GetValue,
> so I can't see where it would be copied to. Unless embedded dispatch
> requires repeated copies (in which case, the implementation is extremely
> dumb, and should be rethought, but I don't think this is the case), I can't
> see how the method call would result in the [1000]byte being copied.
Sorry for the confusion.
Andrew