Io.reader and Io.writer in the go language and their implementation

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

In the process of using the go language, whether you are implementing a Web application, console input or output, or network operation, you will inevitably encounter IO operations and use it to IO. Reader and Io.writer interface. may also be familiar with these two interfaces and some related interfaces, but it is very difficult in your mind to form an IO interface inheritance relationship throughout the day, because the Godoc default does not show the official library inheritance as Javadoc, which led to our memory of the IO interface inheritance is not deep, In the use of time also often need to go through the document to deepen memory. This article attempts to comb through the inheritance relationship of the Go IO interface, providing a complete picture of the IO interface.

Io Interface Review

First, let's review a few common IO interfaces. The implementation of the standard library is to subdivide functionality, each with a minimum granularity function defined as an interface, and then the interface can be composed into a more versatile interface.

Minimum-granularity interface

123
type Interface {    Read (p []byteint, err Error)}
123
type Interface {    Write (p []byteint, err Error)}
123
type Interface {    Close () Error}
123
type Interface {    int64int) (int64, error)}
123
type Interface {    Int64, err Error)}
123
type Interface {    Int64, err Error)}
123
type Interface {    readat (p []byteint64int, err Error)}
123
type Interface {    writeat (p []byteint64int, err Error)}

And

123
type Interface {    readbyte () (byte, error)}
123
type Interface {    byte) error}

Bytescanner a method more than Bytereader UnreadByte .

1234
type Interface {    bytereader    unreadbyte () Error}
123
type Interface {    runeint, err Error)}
1234
type Interface {    runereader    unreadrune () Error}

Combination interface

The Go standard library also defines a number of interfaces that are composed of interfaces from the single function above.

1234
type Interface {    Reader    Closer}
1234
type Interface {    Reader    Seeker}
1234
type Interface {    Reader    Writer}
12345
type Interface {    Reader    Writer    Closer}
12345
type Interface {    Reader    Writer    Seeker}
1234
type Interface {    Writer    Closer}
1234
type Interface {    Writer    Seeker}

As can be seen from their definition, they are a combination of the smallest granularity.

Extension of the minimum interface

Some struct structs implement and extend the interfaces, which are.

1234
type struct {    //Underlying reader    Int64  //Max bytes remaining}
123
type struct {    //contains filtered or unexportedfields}
123
type struct {    //contains filtered or unexportedfields}
123
type struct {    //contains filtered or unexportedfields}

Below I will draw their inheritance relationship.

Some auxiliary methods

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

12345
func Int64) Readerfunc multireader (Readers ... Reader) Reader func teereader (R Reader, W Writer) readerfunc multiwriter (writers ... Writer) writer

Inheritance relationship

Of course, the go language does not have the same inheritance as in Java, but is implemented based on the duck type, and I try to demonstrate the inheritance of the Go IO interface.

Where yellow is the bufio type under the package,
Green is the archive.tar type under the package,
Blue is the bytes type under the bag,
Pink is the strings type under the bag,
Purple is the crypto.tls type under the bag.

RandIs the math.rand type under the package.
Fileis the os content under the package.
The one on the left of ' Rand ' Reader is the image.jpeg content below.

Our most commonly used is the package io , bytes and bufio under the type, so the types of these packages to remember, in the third library often appear in their shadow.

It is not put mime/multipart.File and net/http.File listed, mainly the diagram is too complex, they implement the interface and os.File similar.

Of course you may ask, how do you organize their inheritance relationships? In fact, you can godoc -analysis=type -http=:6060 create a go doc with an inheritance relationship, and it can also show the inheritance relationships in the libraries you download locally.

Reference documents

    1. https://golang.org/pkg/io/
    2. https://golang.org/pkg/bufio/
    3. https://golang.org/pkg/bytes/
    4. https://medium.com/@benbjohnson/go-walkthrough-io-package-8ac5e95a9fbd#.er7vmwvb3
    5. https:// medium.com/@blumenmann/a-simple-beginners-tutorial-to-io-writer-in-golang-2a13bfefea02#.sm0gq7rn0
    6. https://www.reddit.com/r/golang/comments/4z2eo2/noob_question_how_to_discover_interface/

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.