Go Interface 2

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
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 () ErrorType 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 in Terface {Read (buf []byte) (n int, err error)}type Iwriter interface {Write (buf []byte) (n int, err error)}type Icloser int Erface {Close () error}//Although here file does not have a display to inherit the interface, even do not know the existence of these interfaces//However, as long as file implements the function of the interface, you can access and assign a value var file1 IFile = new (File) var File2 ireader = new (file) var file3 iwriter = new (file) var file4 icloser = new (file)


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.


First of all, look at the first method

Interface Integer intfunc (a integer) less (b integer) Bool{return a < B}func (a *integer) Add (b integer) {*a+=b}type Lessadder interface{less (b integer) booladd (b integer)}var a integer=1//yesvar b lessadder = &a//novar b lessadder = A

The reason is that the go language can be based

Func (a integer) less (b integer) Bool{return a < b}

Automatically generated

Func (a *integer) less (b Integer) Bool{return *a < b}
or vice versa.

The second method of


Package Onetype Readwriter Interface{read (buf [] byte) (n int, err error) Write (buf [] byte) (n int, err error)}package Twoty PE IStream interface{read (buf [] byte) (n int, err error) Write (buf [] byte) (n int, err error)}var file1 one. Readwriter = new (File) var file2. IStream = File1var File3 one. Readwriter =file2//because the Iwriter method list is a subset of the Readwrtier method list, you can assign Readwriter to Iwritervar file4 iwriter = file3

Interface Query



Love the Place

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.