I/O operations are also called input/output operations. Where I refers to Input, O refers to Output, which is used to read or write data. In some languages, it is also called stream operation, which is the channel of data communication.

Golang standard library for IO abstraction is very sophisticated, each component can be arbitrarily combined, can be used as a model for interface design.

A, IO package

The IO package provides a series of interfaces for raw I/O operations. It mainly wraps some existing implementations, such as those found in OS packages, and abstracts them into practical functions and other related interfaces.

Because these interfaces and the original operations wrap low-level operations in different implementations, customers should not assume that they are safe for parallel execution.

The two most important interfaces in the IO package are the Reader interface and the Writer interface.

The definition of the Reader interface, the Read() method used to Read data.

Type Reader interface {Read(p []byte) (n int, err error)}COPY Read Reads len(p) bytes into p. It returns the number of bytes read n(0 <= n <= len(p)) and any errors encountered. Even if Read returns n < len(p), it uses all of P as staging space during the call. If some data is available but less than len(p) bytes, Read returns what is available as usual, rather than waiting for more. When Read encounters an error or EOF condition after successfully reading n > 0 bytes, it returns the number of bytes Read. It returns (non-nil) errors from the same call or from subsequent calls (and n == 0). An example of this general case is when Reader returns a non-zero number of bytes at the end of the input stream. It may return either ERR == EOF or Err == nil. In any case, the next Read should return 0, EOF. Callers should always process n > 0 bytes before considering err errors. Doing so correctly handles I/O errors after reading a few bytes, along with the allowed EOF behavior. The implementation of Read prevents returning a count of zero bytes and a nil error, which the caller should treat as a null operation.Copy the code

The definition of the ReaderFrom interface encapsulates the basic ReadFrom method.

type ReaderFrom interface { ReadFrom(r Reader) (n int64, Err Error)}COPY ReadFrom Reads data from r into the data stream of the object until r returns EOF or r has a read error. Return value n is the number of bytes read. Return value err is the return value of RCopy the code

Define the ReaderAt interface, which encapsulates the basic ReadAt methods

Type ReaderAt interface {ReadAt(p []byte, off int64) (n int, err Error)}COPY ReadAt reads data from off of the object data stream to p, If the object's data stream is only partially available and not enough to fill p, the ReadAt waits until all data is available, Continue writing into p until p is full and return at this point ReadAt is more strict than Read and returns the number of bytes Read and the errors encountered while reading if n < len(p), If n = len(p) and the object has not read all of its data, err will return nil. If n = len(p) and the object has just read all of its data, So err will return EOF or nilCopy the code

Other…

Second, file reading

The File class is in the OS package and encapsulates the underlying file descriptor and related information, as well as the Read and Write implementations.

Func (f *File) Read(b []byte) (n int, err Error) // The Read method reads up to len(b) bytes from f and writes to B. It returns the number of bytes read and any errors that may have been encountered. The end of file flag reads 0 bytes and returns the value of err as IO.EOF. Func (f *File) ReadAt(b []byte, off int64) (n int, err Error) //ReadAt reads len(b) bytes from the specified position (relative to the start of the File) and writes to B. It returns the number of bytes read and any errors that may have been encountered. This method always returns an error when n is less than len(b); If the end of the file was reached, the return value err would be io.eof. . COPYCopy the code

Three, example code

Read data from a file:

Package main import (" OS "" FMT" "IO") func main() { Read(p []byte)(n int, error) */ / Open the file fileName: = "/ Users/ruby/Documents/pro/a/aa. TXT" file, err: = OS. Open (fileName) if err! Println("err:",err) return} //step3: Close the file defer file.close () //step2: defer file.close () Bs := make([]byte,4,4) /* // First Read n,err := file.read (bs) ftt.println (err) // ftt.println (n) //4 ftt.println (bs) //[97 98 99 100] fmt.println (string(bs)) //abcd // Read n for the second time,err = file.read (bs) fmt.println (err)// fmt.println (n)//4 Ftt.println (bs) //[101 102 103 104] ftt.println (string(bs)) //efgh // Read n for the third time,err = file.read (bs) ftt.println (err) // Println(n) //2 fmt.println (bs) //[105 106 103 104] fmt.println (string(bs)) //ijgh // Read n for the fourth time,err = file.read (bs) fmt.Println(err) //EOF fmt.Println(n) //0 */ n := -1 for{ n,err = file.Read(bs) if n == 0 || err == io.EOF{ FMT.Println(" Read the end of the file, end the read operation." ) break } fmt.Println(string(bs[:n])) } }Copy the code