Interface combination in Go language (interface included)

Source: Internet
Author: User
This is a created article in which the information may have evolved or changed.

In the go language, one or more interfaces (such as interfaces B, c) can be combined in interface A, which is equivalent to adding a method declared in interface B and C in interface A.

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}


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.