0

I need some clearance of golang behavior. Imagine we have an interface with some method, and we have a type that implements that method. If we assign pointer to type to variable defined as interface, golang allows us to do it.

But when we try to assign slice of pointers to type to the variable defined to contain slice of interfaces, golang panics...

Can anyone explain why?

Here is an example

pupizoid
  • 301
  • 1
  • 12

1 Answers1

0

as it came in here:

they do not have the same representation in memory.

CallMeLoki
  • 1,281
  • 10
  • 23
  • Thanks, i missed that! – pupizoid Jul 28 '18 at 10:37
  • Well, this is a very weak argument if you ask me. I would assume that the collection of interfaces is implemented as something like `std::shared_ptr`. And if we can prove at compile time that some other type `T` conforms to the interface `I`, we can prove that the collection of *pointers* to type `T` can be done by simple casting. And, yes, these two collections will have the same representation in memory because both of them just collections of pointers. Maybe in C++ it's not theoretically true, so it's discouraged and won't compile, but Go certainly could make it work. – Valentin Shergin Jun 19 '21 at 05:29