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