One, custom type
This part seems to be a long time, difficult to understand, difficult to grasp the key points. There are several concepts:
Interface embedding, aggregation and embedding of structures (embedded values, embedding methods with anonymous values, embedded interfaces), free combination of each other independent interface.
Nesting is not inheritance.
The basic embedding is not detailed, the effect of this embedding is a embedded in a B, then a is equivalent to the definition of a B point all the content.
Free combination of each other independent interface, relative to the level of nesting, to be more flexible, and do not need to maintain a hierarchy, you can freely add other interfaces.
Interface embedding, or use a piece of code to say it
Type OptionerInterface{Name ()stringIsValid ()BOOL}type Optioncommonstruct{shortnamestringlongnamestring}type floatoptionstruct{Optioner//anonymous fields, (interface embedding, requires a specific type)Value float64}type genericoptionstruct{Optioncommon//anonymous fields, embedding}func (option genericoption) Name ()string {returnname (option. ShortName, option. longname)}func (option genericoption) IsValid ()BOOL {return true}func Main () {sizeoption:=floatoption{genericoption{optioncommon{"s","size"}},12.3}}
Note that this code, when defining Floatoption, is an embedded interface that, when used, is explicitly assigned a value to satisfy the interface. (interfaces are said to be abstract and cannot be initialized to 0 by default)
Go Language Programming Study (10)