Golang of Learning: interface

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
package main //  defines an interface//  has two methods//  Note: Methods cannot be added functype ifile interface { Read (file string)   (buf []byte) Write (file string, buf []byte)}type ireader  interface {read (file string)   (buf []byte)}type iwriter interface { Write (File string, buf []byte)}//  defines a struct//  by the method defined below:// file also implements IFile  ireader iwriter all the methods//  so File implementation ifile  also implemented Ireader and iwritertype file struct  Methods in {}//  structure func  (f *file)  read (file string)   (buf []byte)  {return Methods in the  nil}//  structure func  (f *file)  write (file string, buf []byte)  {} Func main ()  {f := new (File)// file also implements Ifile ireader iwriter all methods//  So the file implements the ifile  also implements the Ireader and iwriter//  so these transitions are grammatically consistent var f1 ifile = fvar f2  ireader = fvar  F3 iwriter = ff. Read ("AA") F1. Read ("BB") F2. Read ("CC")// iwriter only defined the Write method F3.write ("DD",  []byte {1,2})}


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.