What is the difference between go's type embedding and object composition?
My understanding is that type embedding is object composition except the methods of the embedded type are automatically forwarded as they would be in inheritance.
Software Engineering Stack Exchange is a question and answer site for professionals, academics, and students working within the systems development life cycle. It only takes a minute to sign up.Sign up to join this community
It's exactly as you described it: Go's embedding is composition with the added benefit of "forwarding" the interface.
In my mind embedding is an attempt to preserve the main benefit of traditional OOP inheritance--code reuse--while avoiding its greatest pitfall--extremely tight coupling. When you "inherit" a method via embedding, your "derived" class is not responsible for or even capable of reimplementing it, so it can't possibly "break the base class" somehow. And, since there are no "protected" or "private" methods that your "derived" class might gain access to, there's no violation of encapsulation. But you still have all the convenience of directly "inheriting" the "base class interface" as opposed to writing a bunch of wrapper functions.