On Golang interface

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

The interface design of Golang is one of the two highlights of this programming language, this article only discusses the difference between Golang interface and other languages (in C + + for example).

Referring to the "Go Language programming" author in the book, the interface in the Go language is "non-intrusive", while the interface in other languages is "intrusive". Therefore, it is necessary to understand the advantages of the Golang interface by understanding the non-intrusive and intrusive interpretations separately.

First let's look at the interfaces in C + +:

In C + +, the implementation of an interface must pass through inheritance

"' CPP

interface IFoo{    void Bar();}class Foo: public IFoo{}IFoo *Foo=new IFoo;

In such a language, even if there is another interface IFoo2 implementation of the same interface method as IFoo, even if the name is the same only in different namespaces, the compiler will assume that the above class Foo only implements the IFoo interface, and does not implement the IFoo2 interface

Such an interface is called an intrusive interface. The main manifestation of "intrusive" is that the implementation class must explicitly indicate that it implements an interface. In C + + it is expressed as needing inheritance.

To cite a specific example:

#include  <iostream>using namespace std;class person{    public:     person (): M_strname ("# # #") {};    virtual void setname ( Const string name) =0;    private:    string m_strname;}; Class person2{    public:    person2 (): M_strName ("# # #") {};     virtual void setname (Const string name) =0;     private:    string m_strname;}; Class student:public person{    public:    student (): m_ StrName ("* * *") {};    void setname (const string name);     private:    string m_strName;}; Void student::setname (const string name) {    std::cout << m _strname <<  std::endl;    m_strname=name;    std::cout << m _strname << std::endl;} Int main ()  {    Student s;    Person *p;     p=&s;    Person2 *p2;    p2=&s;     return 0;}

In this case, because student does not declare the inheritance Person2 so for p2= $s; This code will appear: "Cannot convert struden* to Person2 in assignment" error.

In the go language, a class implements the interface as long as it implements all of its functions.

type file struct{}func(f *File) Read(buf []byte)(n int, err error)func(f *File) Write(buf []byte)(n int, err error)func(f *File) Seek(off int64, err eror)(pos int, err error)func(f *File) Close() err errortype IFile interface{    Read(buf []byte)(n int, err error)    Write(buf []byte)(n int, err error)    Seek(off int64, err eror)(pos int, err error)    Close() err error}type IReader interface{Read(buf []byte)(n int, err error)}type IWriter interface{ Write(buf []byte)(n int, err error)}type ICloser interface{ Close() err error}

Although the file class does not inherit from these interfaces, it can even be unaware of the existence of these interfaces, but file implements all the methods of these interfaces, it can be said that the file class implements these interfaces, can be assigned

In fact, we can use a popular example to explain:

For intrusive interface, suppose you are in the college entrance examination, but not the national exam, but the university autonomy proposition, at this time there are x university and Y University two schools, their exams are exactly the same, the admission score is exactly the same, but even if you took the X-University exam, reached the X-University scores, You can not apply to go to the Y University, to apply for the Y University, you must again take the Y University exam and meet the Y University's Bar, here to take the exam and reach the bar is equivalent to the inheritance in C + +, if there is no declaration of inheritance, it can not be explained that the implementation of an interface

And for the non-intrusive interface, the same is the college entrance examination, but this is the national test, you pass the score, this time you can apply for X university, but also can apply for Y University, because you have reached the two universities require a unified score, equivalent to you achieve the X and Y two interface all methods, so can call X and can call Y

The key to the difference between intrusive and non-intrusive interfaces is whether you need to explicitly declare that your class implements an interface.

The above represents the individual understanding, for the wrong or inappropriate places, welcome criticism and correction.

1410 reads ∙2 likes
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.