This is a creation in Article, where the information may have evolved or changed.
Introduced
Package IO mainly contains the following files:
- Io.go Basic IO operations, such as read-write, lower-level
- Multi.go multiple reader packages into a single
- Pipe.go
- Ioutil Common operations
- The Ioutil.go encapsulates the Ioutil
- Tempfile.go about creating temporary files
Reader & Writer
These two interfaces are one of the most widely used interfaces, so focus on learning, followed by a variety of commonly used packages such as os,net.
io.Readertype Reader interface { Read(p []byte) (n int, err error)}io.Writertype Writer interface { Write(p []byte) (n int, err error)}
Specific examples to strings. Reader For example, code:
package strings...type Reader struct { s string i int64 // current reading index prevRune int // index of previous rune; or < 0}func (r *Reader) Read(b []byte) (n int, err error) { if r.i >= int64(len(r.s)) { return 0, io.EOF } r.prevRune = -1 n = copy(b, r.s[r.i:]) r.i += int64(n) return}
This class can be seen to satisfy IO. Reader contract, so you can use this (for clarity, explicit type encoding):
var strReader io.Reader = strings.NewReader("hello")if _, err := io.Copy(os.Stdout, r); err != nil { log.Fatal(err)}
From the source point of view,
First, in fact, reader is a container that encapsulates the serialized data. This may not be so obvious from the naming point of view, it may be that reader is just a caller and need to "read" the data from somewhere else, in fact most of the reader itself includes the data.
Second, reader is similar to our current Youku Tudou player, able to remember the location of the last play, every time we go to watch from the last look.
Copy
This method can also be seen from the front of the very common
func Copy(dst Writer, src Reader) (written int64, err error)
The function is to write the contents of SRC into DST,
Note that there is a copy method similar to the one in Buildin,
Func copy (DST, src []type) int
It's just dealing with []byte type.
View IO. Copy method of the source code, we found that called the
func copyBuffer(dst Writer, src Reader, buf []byte) (written int64, err error)
And the default is to create a buf, the code is as follows
if buf == nil { buf = make([]byte, 32*1024)}
The SRC is then called internally. Read (BUF) and gradually writes BUF content to DST if it returns no EOF. Plus some error handling.
The Copy method also has a brother: Io.copyn:
func CopyN(dst Writer, src Reader, n int64) (written int64, err error)
A maximum of n bytes is copied.
As opposed to Copyn, there is one more way:
func ReadAtLeast(r Reader, buf []byte, min int) (n int, err error)
The method reads at least min bytes until it is all read, otherwise it throws an exception.
The main purpose of this method is to ensure that the contents of byte are fully read, see Readfull:
func ReadFull(r Reader, buf []byte) (n int, err error) { return ReadAtLeast(r, buf, len(buf))}
Io/ioutil
In contrast to the partial-bottom implementation of Io, the Ioutil method is more practical, and the common methods are:
func ReadAll(r io.Reader) ([]byte, error)
Reads all the contents of reader, returns a byte array, does not need to provide a byte array yourself
func ReadFile(filename string) ([]byte, error)
Pass in a file name, return the content byte array
func WriteFile(filename string, data []byte, perm os.FileMode)Error
Writes data to the file, and if the file does not exist, creates the file in Perm mode
func ReadDir(dirname string) ([]os.FileInfo, error)
Returns all file information in the directory (personally think this method appears a bit strange, should be more reasonable in the OS package)