The detailed and realization of io.reader and io.writer in Go language _golang

Source: Internet
Author: User
Tags inheritance rand

First, the preface

It may be familiar to these two interfaces and some of the related interfaces, but it's hard to get a picture of an inherited relationship to an IO interface all day long, because godoc default does not show the official library inheritance relationship like Javadoc, which leads to a memory of our inheritance relationship with IO interfaces. In the use of time also often need to turn documents to deepen memory.

This paper attempts to comb through the inheritance relationship of the Go IO interface and provide an overview of IO interfaces.

Second, IO interface review

First we review a few common IO interfaces. The implementation of the standard library is to subdivide the functionality, defining the function of each minimum granularity as an interface, and then the interface can be made into a more versatile interface.

Interface with minimum granularity

Type Reader Interface {
  Read (p []byte) (n int, err error)
}
Type Writer Interface {
  Write (p []byte) (n int, err error)
}
Type Closer Interface {close
  () error
}
Type Seeker interface {seek
  (offset int64, whence int) (int64, error)
}
Type Readerfrom Interface {
  Readfrom (r Reader) (n Int64, err error)
}
Type Writerto Interface {
  WriteTo (w Writer) (n Int64, err error)
}
Type Readerat Interface {
  readat (p []byte, off Int64) (n int, err error)
}
Type Writerat Interface {
  writeat (p []byte, off Int64) (n int, err error)
}

And

Type Bytereader Interface {
  readbyte () (Byte, error)
}
Type Bytewriter Interface {
  writebyte (c byte) error
}

There is one more way to Bytescanner than Bytereader UnreadByte .

Type Bytescanner interface {
  bytereader
  unreadbyte () error
}
Type Runereader Interface {
  readrune () (R rune, size int, err error)
}
Type Runescanner interface {
  runereader
  unreadrune () error
}

Composite interface

The Go standard library also defines some interfaces that are composed of a single functional interface above.

Type Readcloser interface {
  Reader
  Closer
}
Type Readseeker interface {
  Reader
  seeker
}
Type Readwriter interface {
  Reader
  Writer
}
Type Readwritecloser interface {
  Reader
  Writer
  Closer
}
Type Readwriteseeker interface {
  Reader
  Writer
  seeker
}
Type Writecloser interface {
  Writer
  Closer
}
Type Writeseeker interface {
  Writer
  seeker
}

As can be seen from their definitions, they are a combination of minimal granularity.

Extension of the minimum interface

Some structures are struct implemented and extend interfaces, which are.

Type limitedreader struct {
  R reader//underlying reader
  N Int64//Max bytes remaining
}
Type Pipereader struct {
  //contains filtered or unexported fields
}
Type pipewriter struct {
  //contains filtered or unexported fields
}
Type Sectionreader struct {
  //contains filtered or unexported fields
}

I'll draw their inheritance relationships below.

Some auxiliary methods

Some helper methods can generate special types of reader or writer:

Func Limitreader (R reader, n Int64) reader
func multireader (Readers ... Reader) Reader
func Teereader (R reader, W Writer) Reader
func multiwriter (Writers ... Writer) Writer

Iii. the relationship of inheritance

Of course, the go language does not have the inheritance relationship in Java, but it is based on the Duck type form, and I try to show the inheritance of the Go IO interface using the diagram below.

Where yellow is the type under the Bufio package,

Green is the type under the Archive.tar package,

Blue is the type under the bytes package,

Pink is the type under the strings package,

Purple is the type under the CRYPTO.TLS package.

Rand is the type under the Math.rand package.

File is the content under the OS package.

The reader on the left of ' Rand ' is the content under Image.jpeg.

We are most commonly used in the package Io, bytes, Bufio type, so the types of these packages to remember, in the third library often appear in their figure.

There is no mime/multipart in the picture above. File and Net/http. The file is listed, mainly because the graphs are too complex, the interfaces they implement, and the OS. File is similar.

Of course you may ask, how do you sort out their inheritance relationships? In fact, you can generate go doc with inheritance by godoc-analysis=type-http=:6060, and it can also show the inheritance in your locally downloaded library.

Iv. Summary

The above is about the go language Io.reader and io.writer the full content of the details and implementation, I hope this article content for everyone's study and work can help, if there is doubt can message exchange.

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.