Original post

Hi Gophers,

I’m a bit confused today after reviewing a few design pattern implemented in and trying to translate this in a classic UML Class Diagram.

My understanding of the language is there’s no inheritance but embedding. An embedding struct/interface can use the methods and fields of another “base” struct/interface.

Some guys are talking about embedding as aggregation. So I’ve made some research and it reminds me of the confusion between aggregation and composition that I encountered a long time ago in Java.
From my standpoint, the more pragmatic answer to make the difference is that with composition, if no more references to a particular instance of A exist, its instance of B is destroyed which is not the case for aggregation.

Embedding is done by referencing the struct type without assigning a name for the field. Internally, it’s the name of the type of the struct that is used like below

type Car struct {
    Engine
}

If I add a field name then it’s no more embedding.

So, from my standpoint, it’s either composition or aggregation.
If the Engine struct is used by copy then it’s composition cause the instance will be destroyed once the Car instance will be destroyed.
If the Engine struct is a pointer, then it depends on whether the Engine type is used elsewhere or not.

Am I right or am I missing something?

type Car struct {
    e Engine
}

type Car struct {
    e *Engine
}

Thank you in advance for your help.