This is a creation in Article, where the information may have evolved or changed.
began to understand what is anonymous members, read several articles, finally understand, at least self-think is.
Why do we need anonymous members?
Have to start with OO. OO is a necessary part of modern computer programming languages. However, different languages are not the same for how to implement various concepts in OO. Go provides support for OO, but it is a neat and straightforward approach that does not resemble the extends, implements keywords in Java, but rather uses anonymous members directly and is implemented through "delegation." For example, the following code:
Type E interface{
Start ()
Stop ()
Renewal (x int)
}
Type W Interface {
Open ()
Close ()
Renewal ()
}
Type Car struct {
E
W
}
With this, when you have created an instance of car, you can use:
Car. Start ()
Car. Stop ()
Car. Open ()
Car. Close ()
There is no difference between using car and your own method. Is there extends and implements mean!
It is important to note that this is not possible due to the presence of the same name Method (renewal):
Car. Renewal ()//Compilation error
and must be:
Car. E.renewal (5)
Or
Car. W.renewal ()
It seems that with this anonymous member and delegation, several concepts in OO are included.