This article illustrates the implementation of the interface combination in go language. Share to everyone for your reference. The implementation methods are as follows:
In the go language, you can combine one or more other interfaces (such as interface B, c) in interface A, which is equivalent to adding a method declared in interface B and C in interface A.
Copy Code code as follows:
Interfaces can be combined in a way that is equivalent to adding other interfaces to the interface
Type Reader Interface {
Read ()
}
Type Writer Interface {
Write ()
}
Defines the implementation classes for both of these interfaces
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 {
Reader
Writer
}
The above interface is equivalent to:
Type ReadWriterV2 Interface {
Read ()
Write ()
}
Readwriter and ReadWriterV2 Two interfaces are equivalent, so you can assign values to each other
Func interfaceTest0104 () {
MRW: = &myreadwrite{}
The MRW object implements the Read () method and the Write () method, so you can assign values to Readwriter and ReadWriterV2
var rw1 readwriter = MRW
Rw1.read ()
Rw1.write ()
Fmt. PRINTLN ("------")
var rw2 ReadWriterV2 = MRW
Rw2.read ()
Rw2.write ()
At the same time, Readwriter and ReadWriterV2 two interface objects can be assigned to each other
RW1 = RW2
RW2 = Rw1
}
I hope this article will help you with your go language program.