News for hackers. Stuff that matters.
Re:Why I like Golang's interfaces by echlebek on Friday April 14, 2017 @02:45PM (#14116274)
Your code doesn't contain the definition of Foo and Fooer but I'll assume Foo is a concrete type and Fooer is an interface that Foo satisfies.

The reason that []Foo is not the same as []Fooer is simple: they are different data types. They have a different layout in memory.

Internally, an interface value is a pair of pointers. One points to the concrete value, and the other points to the concrete type of the value.

The compiler needs to know the size of the things it's working with in the case of slices, because it needs to know how to compute offsets for indexing and iteration. It could make an implicit copy at runtime, but that could trigger a costly operation that would be invisible to the user. Go wants expensive operations to be visible, so you have to make the copy yourself.

There are no comments on this comment yet.