Interface to go language programming (15)

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

Interface plays a critical role in the go language. If the Goroutine and channel are the cornerstone of the concurrency model that supports the go language, making the go language an extremely beautiful landscape in today's clustered and multicore era, the interface is the cornerstone of the whole type of Go language system, Let the go language in the exploration of basic programming philosophy to reach an unprecedented height.

The go language is a transformational faction, not an improvement, in programming philosophy. This is not because the go language has goroutine and channel, but more importantly because of the go language type system, but also because of the go language interface. The programming philosophy of the Go language is almost perfect because of its interface.
The interface of the go language is more than just an interface, so let's explore the interface features of the go language through a series of comparisons.

Interfaces for other languages

The interface of the go language is not the interface concept provided in other languages (C + +, Java, C #, etc.).

Prior to the advent of the Go language, the interface was primarily a contractual existence between different components. The implementation of the contract is mandatory, and you must declare that you did implement the interface. In order to implement an interface, you need to inherit from the interface:

InterfaceIFoo{
 voidBar();
}
ClassFooimplementsIFoo{//   Java
    //...
}
ClassFoo:publicIFoo{// C + + 
    //...
}
IFoo*foo=newfoo;
Even if there is an interface IFoo2 implements the same interface method as IFoo and even the name IFoo is located in a different namespace, the compiler will assume that the above class Foo only implements the IFoo and does not implement the IFoo2 interface.

Such interfaces are what we call intrusive interfaces. The main manifestation of "intrusive" is that the implementation class needs to explicitly declare that it implements an interface.

Non-intrusive interface

In the go language, a class only needs to implement all the functions required by the interface, and we say that this class implements the interface, for example:

TypeFilestruct{
    //...
}
func     ( f  *  file  )    read ( buf  [] . Span style= "COLOR: #000080" >byte  )    Span style= "COLOR: #000000" > ( n  int  Span style= "COLOR: #000000",    err  Span style= "COLOR: #000080" >error   
func     ( f  *  file  )    write ( buf  []  byte  )     ( n< Span style= "COLOR: #c0c0c0" >  int  ,  Span style= "COLOR: #c0c0c0" >  err  error   
func     ( f  *  file  )    seed ( off  int64      whence  int  )     ( pos  int64     err  error  )  
Func(f*File)CLose (  )  Error
Here we define a file class and implement methods such as read (), Write (), Seek (), Close (), and so on. Imagine that we have the following interfaces:

TypeIFileinterface{
read ( buf  []  byte  )     ( n  Span style= "COLOR: #000080" >int     err   error  )  
write ( buf  []  byte  )     ( n  int  ,    Err  error  )  
Seek(offInt64,whenceint)( PosInt64,errerror)
Close()error
}
Typeireaderinterface{
Read(buf[]byte)(nint  ,errerror)
}
Typeiwriterinterface{
Write(buf[]byte)(nint  ,errerror)
}
Typeicloserinterface{
Close()error
}
Although the file class does not inherit from these interfaces, it can even be unaware of the existence of these interfaces, but the file class implements these interfaces and can be assigned values:

varfile1IFile= New (File)
varfile2ireader=new(File )
varfile3iwriter= New (File)
varfile4icloser=new(File )
The go language's non-intrusive interface has the following advantages:

First, the Go Language standard library, no longer need to draw the class Library inheritance tree diagram.

Second, the implementation of the class, only need to care about what they should provide, no longer entangled in the interface need to dismantle the more fine to be reasonable. Interfaces are defined on demand by the consumer without prior planning.

Thirdly, it is not necessary to import a package in order to implement an interface.



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.