This is a creation in Article, where the information may have evolved or changed.
The interface of the go language is not the interface concept provided in other languages (C + +, Java, C #, etc.).
based on the principle of one-way dependency of module design , module A should not be concerned with the requirements of a specific user when implementing its own business. When the template A is implemented, it is not even known that it will be used by today's B.
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。
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 (o FF int64, whence int) (POS int64, err Error) func (f *file) Close () error
The file class implements 4 methods If you have an interface:
Type IFile Interface { Read (buf []byte) (n int, err error) Write (buf []byte) (n int, err error) Seek (off Int64 , whence int) (POS int64, err Error) Close () Error}
Type Ireader Interface { Read (buf []byte) (n int, err error)}type Iwriter interface { Write (buf []byte) (n int, er R error)}type Icloser interface { 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:
var file1 IFile = new (file) var file2 ireader = new (file) var file3 iwriter = new (file) var file4 icloser = new (file)
Because the file class implements all of the methods required by the interface
Interface Assignment Value
The interface assignment is divided into the following two scenarios in the Go language:
- Assigning an object instance to an interface;
- Assigns an interface to another interface.
Type Integer int<pre name= "code" class= "plain" >func (a integer) less (b integer) bool { return a < b}
Func (a *integer) Add (b Integer) {*a + = b}
Defining interfaces
Type Lessadder Interface {less (b integer) bool Add (b Integer)}
var a Integer = 1var b lessadder = &a ... (1) OK <span style= "font-family:arial, Helvetica, Sans-serif;" ></span>var b lessadder = a ... (2) No
</pre><pre name= "code" class= "plain" >go can automatically generate new method func (a *integer) based on func (a integer) less (b integer) bool Less (b Integer) bool { return (*a). Less (b)}
An interface assignment does not require two interfaces to be equivalent. If the method list of interface A is a subset of the method list of interface B, then interface B can be assigned to interface A.
Interface Query
var file1 Writer = If file5, OK: = File1. (both. IStream); OK { ...}
This if statement checks whether the object instance pointed to by the File1 interface implements both. IStream interface.
In the go language, you can ask the interface whether the object it points to is a type, such as:
var file1 Writer = If file6, OK: = File1. (*file); OK {//This if statement determines whether the object instance pointed to by the File1 interface is *file type ... }
Type query
In the go language, you can also ask a more straightforward question about the type of object instances that the interface points to, such as:
var v1 interface{} = ... switch V: = v1. (type) {Case int://The type of V is now int case string://Now the type of V is string ...}
Interface Combination
Interfaces can be combined in a way that is equivalent to the method of adding other interfaces to the interface type Reader interface {read ()}type Writer Interface {write ()}//defines the implementation class of the two interfaces above type Myreadwrite Struct{}func (MRW *myreadwrite) Read () {FMT. Println ("Myreadwrite...read")}func (MRW *myreadwrite) write () {FMT. Println ("Myreadwrite...write")}//defines an interface that combines the above two interfaces type Readwriter interface {readerwriter}//The above interfaces are equivalent to: type ReadWriterV2 interface {Read () write ()}//readwriter and ReadWriterV2 two interfaces are equivalent, so can be assigned to each other Func interfaceTest0104 () {MRW: = The &MYREADWRITE{}//MRW object implements the Read () method and the Write () method, so you can assign values to Readwriter and readwriterv2var rw1 readwriter = Mrwrw1.read () Rw1.write () fmt. PRINTLN ("------") var rw2 ReadWriterV2 = Mrwrw2.read () rw2.write ()//At the same time, Readwriter and ReadWriterV2 two interface objects can be assigned to each other RW1 = Rw2rw2 = RW1}
Any type
because any object instance in the go language satisfies the null interface interface{}, interface{} looks like an any type that can point to any object, as follows:
var v1 interface{} = 1//assign int type to Interface{}var v2 interface{} = "ABC"//Assign String type to Interface{}var v3 interface{} = &A MP;V2//Assign *interface{} type to Interface{}var v4 interface{} = struct{x int}{1}var v5 interface{} = &struct{x int}{1}