Go Language Programming Study (10)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.