The idea of interface in go language

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

In the Go language, interface fades the symbolic meaning of interface in object-oriented, and the interface in go language is merely the abstract concept of the same kind of things as "expression form". In the go language, as long as the "type" with the same "expression" has the same interface, it does not need to consider the practical significance of this interface in the specific use. In this way, the cost of interface is given to the user, not to the designer.

The designer is only responsible for the design method, that is, the constituent elements of interface, interface in the specific use of the actual meaning should be given to interface users to determine. To give the simplest example:

Running hypothesis has two meanings: one is escape, English is escape, the other is running, English is called run. If Chinese is a kind, call him pao.

Type Badman struct{}func (Badman *badman) Pao () {}type Sportman struct{}func (sportman *sportman) Pao () {}//Designers only consider this part of type Pao interface{    Pao ()}//This section is considered by the user var Paoa Pao = new (Badman) var paob Pao = new (Sportman)

Although the two types of Pao interfaces actually have different meanings, for the go language, I think their interfaces are the same, because the "form of expression" is the same. As for what scenario should go to use Badman or Sportman Pao interface in order to complete the task correctly, this is the user consideration, design interface people do not consider.


Go's interface is closer to the essence of the concept of interface, because it can be grouped into this interface population for the type of any method that implements the interface type. In object-oriented design language, type must be safe to implement interface content to be managed by it, otherwise it is an abstract class. The type of the object of birth is still a cloth.

On the path of abstraction and instantiation, the go language one step, and if Java and CPP want to one step, then it is necessary to implement all interface methods.

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.