On 4 July 2014 19:45, Peter Kleiweg wrote:Op vrijdag 4 juli 2014 16:04:22 UTC+2 schreef David Symonds:
If I read correctly, a value of type interface is a wrapper with a pointer
to the real thing.
Or with the real thing itself, if it will fit in a pointer-sized slot.
So when I would use a pointer to the real thing as an argument to a
function, I can get the same effect by passing the interface value itself,
because it already is a pointer (conceptually).
Is this correct?
No. A pointer will let you update the variable it points to. The value
in the interface is a copy of the thing it was assigned from, just like
ordinary assignment/parameter-passing.
Also, interfaces give you polymorphism, and pointers do not.
If so, is this part of the language design, or is it just how it happens to
be implemented in the Go compiler. In other words, can I rely on an
interface value always acting like a pointer to the real thing, or might it
change in another implementation?
You can rely on the value stored in an interface being a copy, just like
a variable of the appropriate type.
And you can probably rely on the implementors not randomly discarding
a nice space optimisation.
Chris
--
Chris "allusive" Dollin
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit
https://groups.google.com/d/optout.