The standard library of Go provides several methods to read data from
ioutil), and this article demonstrates the application scenarios by reading data from
Accessing a website using a
TCP connection, using the
HTTP 1.0 protocol, keeping the
TCP connection short and closing it after reading the
response, thus simulating the
io.Reader interface defines the
Read(p byte) (n int, err error) method, which we can use to read a batch of data from the
- Read up to
len(p)length of data at a time
- If the read encounters
io.EOFor other errors), the number of bytes of data read and
errorwill be returned.
- Even if the number of bytes read < len(p), it will not change the size of
p(obviously, because normally it can’t change it)
- When the input stream ends, calling it may return
err == EOFor
err == niland
n >= 0, but the next call will definitely return
n = 0,
err = io.EOF
Often this method is used to read data in bulk from the input stream until the input stream is read to the head, but it is important to note that we should always process the n bytes read first, and then process the error.
Reader interface, when implementing the
Read method, it is not recommended that you return
0,nil unless the input
len(p)==0, because this can cause confusion, and generally use
0,io.EOF to indicate that the input stream has been read to the head.
The following is an example demonstrating this method.
func Copy(dst Writer, src Reader) (write int64, err error) copies data from
dst (the Go implementation generally puts the destination argument in front of the source argument) until.
- Other errors
It returns the number of bytes read and the first error encountered
Note that after successfully reading the input stream
err is not
It does this internally by calling
CopyBuffer(dst Writer, src Reader, buf byte) (write int64, err error).
CopyBuffer may use a specified
buf to copy the data, if the length of
0, it will
In two special cases
buf will not be used, but copied using the methods of the interface.
- src implements the WriterTo interface
- dst implements the ReadFrom interface
ReadAll(r io.Reader) (byte, error) provides a method to read all the data from the input stream until an error (
error) or a read to the head (
A successful header read does not return
io.EOF, but rather the data and
We often use it to read the trusted
ReadFull(r Reader, buf byte) (n int, err error) reads exactly
len(buf) bytes of data from the input stream.
0bytes and encounter
- Read to
0<n<len(buf)bytes and encounter
n==len(buf), even if
erroris encountered, return
err ! = nilthen definitely
ReadAtLeast(r Reader, buf byte, min int) (n int, err error) Reads at least
min bytes from the input stream into
buf is full or
error is encountered, including
- If n < min, an error must be returned
- No data to read returns n=0 and EOF
- n < min, and EOF is encountered, return n and ErrUnexpectedEOF
- If min > len(buf), return 0, ErrShortBuffer
- If at least min bytes are read, return err=nil even if an error is encountered
LimitReader(r Reader, n int64) Reader returns a
Reader with restricted content length, when
n bytes are read from this
Reader for a while it will encounter
Imagine if you didn’t have this protection, say you were sent a picture and it ended up sending you a video of 3G size, which could potentially spike your memory.
It is to provide a protection function, the rest is no different from the ordinary Reader.