This paper is mainly for the study of "Go Language standard library" Https://books.studygolang.com/The-Golang-Standard-Library-by-Example notes
IO operations in the Go language are encapsulated in the following 4 packages:
1) IO: Provides a basic interface for IO primitives (I/O primitives).
2) Io/ioutil: Encapsulates some practical I/O functions.
3) FMT: Implementation of formatted I/O.
4) Bufio: Implement buffered I/O.
1. io--Basic IO interface
The main content of the package is to define a lot of IO-related interface, as the parameters of other functions in the standard library appear, wherein the most basic important two interface is IO. Reader and Io.writer.
Type Reader Interface { Read (p []byte) (n int, err error)}
Official Note:
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 the N < Len (p), it also takes Len (p) bytes as the staging space during the call.
If the data that can be read is less than Len (p) Bytes, read returns the available data instead of waiting for more data.
When read encounters an error or EOF (End-of-file) after successfully reading n > 0 bytes, it returns the number of bytes read.
It may return an non-nil error in this call at the same time, or return the error in the next call (and N is 0).
In general, reader returns a non-0-byte number n, and if n = Len (p) bytes are returned from the end of the input source by read, read may return err = = EOF or err = = Nil. and the subsequent Read () should return (n:0, err:eof).
The caller should first process the returned data before considering the error. Doing so correctly handles I/O errors resulting from reading some bytes, while allowing EOF to occur.
Type Writer Interface { Write (p []byte) (n int, err error)}
Official Note:
Write writes Len (p) bytes from p to the basic data stream. It returns the number of bytes written from P, n (0 <= n <= len (P)), and any errors encountered that caused the write to stop prematurely.
If Write returns the N < Len (p), it must return a non-nil error.
Which of these interface are implemented in the standard library? Here are a few:
* OS. File also implements Io.reader and Io.writer.
* Strings. Reader implements the Io.reader
* Bufio. Reader/writer implemented Io.reader and Io.writer respectively.
* bytes. Buffer achieves both Io.reader and Io.writer
* bytes. Reader implements the Io.reader
More information can be accessed http://docs.studygolang.com./pkg/queries.
Some other column interfaces:
Type Readerat Interface { readat (p []byte, off Int64) (n int, err error)}type Writerat interface { writeat (p []byt E, off Int64) (n int, err error)}type Readerfrom interface { Readfrom (r Reader) (n Int64, err error)}type Writerto int Erface { WriteTo (w Writer) (n Int64, err error)}type Seeker interface { Seek (offset int64, whence int) (ret int64, Err error)}type Closer interface { Close () Error}type Bytereader interface { readbyte () (c byte, err error)}type Bytewriter Interface { writebyte (c byte) error}
In addition there are Bytescanner, Runereader, Runescanner, Readcloser, Readseeker, Readwritecloser, Readwriteseeker, ReadWriter, Writecloser, Writeseeker.
Read more https://books.studygolang.com/The-Golang-Standard-Library-by-Example/chapter01/01.1.html
2. ioutil--convenient set of IO operation functions
1) Nopcloser function
Will IO. The instance of reader is converted to an Io.readcloser instance.
2) ReadAll function
Func ReadAll (R io. Reader) ([]byte, error)
Read IO at once. The data in reader
3) Readdir function
Reads the directory and returns the sorted file and subdirectory names.
4) ReadFile and WriteFile functions
Func ReadFile (filename string) ([]byte, error)
ReadFile reads the data from the file specified by filename and returns the contents of the file. The successful call returns ERR as nil rather than EOF. Because this function is defined to read the entire file, it does not treat the EOF returned by the read as an error that should be reported.
Func WriteFile (filename string, data []byte, Perm OS. FileMode) Error
WriteFile writes data to the filename file, and when the file does not exist, it is created according to the permissions specified by perm, and the file contents are emptied first when the file is present. For the perm parameter, we can generally specify as: 0666, the specific meaning of the OS package explained.
5) TempDir and Tempfile functions
Create a temporary directory.
3. fmt--Format IO
4. bufio--Cache IO
1) Bufio. Reader
Packed with an IO. Reader object, which provides caching capabilities while implementing IO. Reader interface.
Type Reader struct { buf []byte //cache Rd io. Reader //Bottom-level IO. Reader //r: Bytes read from buf (offset), offset in w:buf, //W-r is the length that can be read in BUF (the size of the cached data), and the return value of the buffered () method R, W int ERR error// errors encountered during reading lastbyte int //Last read byte (readbyte/unreadbyte) lastrunesize int //The size of the last read Rune (Readrune/unreadrune)}
2) Bufio. Writer
Packed with an IO. Writer object that provides caching functionality while implementing IO. Writer interface.
Type Writer struct {Err error// errors encountered during write buf []byte //cache n int //number of bytes in the current cache WR io. Writer //Bottom-level IO. Writer Object}