Implementation method of interface combination in go language _golang

Source: Internet
Author: User

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.

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.