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.