With use
type ReadCloser interface { Reader Closer }
type nopCloser struct { } func (nopCloser) Close() error { return nil } // NopCloser returns a ReadCloser with a no-op Close method wrapping // the provided Reader r. func NopCloser(r ) { return nopCloser{r} }
Return one
,and
close()
The method has no operationno-op
。
We're operatingreq *
andresponse *
Sometimes you need to read itBody
, but after reading itBody
It is cleared, so we need to reassign the read content toBody
。
Its type is。
func(response *) error { cont, _ := () (string(cont)) = ((cont)) return nil }
ioutil and log packages in golang
- func NopCloser
func NopCloser(r )
NopCloser wraps r with an operationless Close method to return a ReadCloser interface.
- func ReadAll
func ReadAll(r ) ([]byte, error)
ReadAll reads data from r until EOF or encounters error, returns the read data and the error encountered.
The successful call returns err as nil instead of EOF.
Because this function is defined as reading r until EOF, it does not treat the EOF returned by reading as an error that should be reported.
- func ReadFile
func ReadFile(filename string) ([]byte, error)
ReadFile Reads data from the file specified by filename and returns the contents of the file.
The successful call returns err as nil instead of EOF.
Because this function is defined as reading the entire file, it does not treat the EOF returned by reading it as an error that should be reported.
- func WriteFile
func WriteFile(filename string, data []byte, perm ) error
The function writes data to the file specified by filename.
If the file does not exist, the file will be created with the permissions given, otherwise the file will be cleared before writing the data.
- func ReadDir
func ReadDir(dirname string) ([], error)
Returns an ordered list of directory information for dirname specified directory.
- func TempDir
func TempDir(dir, prefix string) (name string, err error)
Create a new temporary folder with prfix as prefix in the dir directory and return the path to the folder.
If dir is an empty string, TempDir uses the directory used for temporary files by default (see function).
Different programs call this function at the same time to create different temporary directories. The program that calls this function is responsible for destroying it when it does not require a temporary folder.
- func TempFile
func TempFile(dir, prefix string) (f *, err error)
Create a new temporary file prefix in the dir directory, open the file in read-write mode and return the pointer.
If dir is an empty string, TempFile uses the directory used for temporary files by default (see function).
Different programs call this function at the same time to create different temporary files. The program that calls this function is responsible for destroying it when it does not require temporary files.
- io func (*PipeReader) Read
func (r *PipeReader) Read(data []byte) (n int, err error)
Read implements the standard Reader interface: it reads data from the pipeline and blocks until the write end starts writing or the write end is closed.
- func (*PipeReader) Close
func (r *PipeReader) Close() error
Close closes the reader; if a write operation is performed on the write end of the pipeline after closing, it will return (0, ErrClosedPip).
- func (*PipeReader) CloseWithError
func (r *PipeReader) CloseWithError(err error) error
CloseWithError is similar to the Close method, but changes the error returned when calling Write to err.
- type PipeWriter
type PipeWriter struct { // Contains hidden or non-exported fields}
PipeWriter is the write end of a pipeline.
- func (*PipeWriter) Write
func (w *PipeWriter) Write(data []byte) (n int, err error)
Write implements the standard Writer interface: it writes data into a pipeline and blocks until the reader has finished reading all the data or the reader is closed.
- func (*PipeWriter) Close
func (w *PipeWriter) Close() error
Close closes the writer; if the read operation is performed on the read end of the pipeline after closing, it will return (0, EOF).
- func (*PipeWriter) CloseWithError
func (w *PipeWriter) CloseWithError(err error) error
CloseWithError is similar to the Close method, but changes the error returned when calling Read to err.
- func TeeReader
func TeeReader(r Reader, w Writer) Reader
TeeReader returns a Reader interface that writes the data it reads from r to w.
All readings to r through this interface will perform corresponding writes to w.
No internal buffering: writes must be completed before reads are completed.
Any errors encountered during writing will be returned as read errors.
- func MultiReader
func TeeReader(r Reader, w Writer) Reader
MultiReader returns a Reader interface that logically connects the provided Reader.
They are read in turn. Read will return EOF only when all input streams are read.
If either of the readers returns an error that is non-nil or non-EOF, the Read method returns the error.
- func MultiWriter
func MultiWriter(writers ...Writer) Writer
MultiWriter creates a Writer interface that writes the data provided to it to all Writer interfaces provided at creation.
- func Copy
func Copy(dst Writer, src Reader) (written int64, err error)
Copy the data of src to dst until EOF is reached on src or an error occurs.
Returns the number of bytes copied and the first error encountered.
For successful calls, the return value err is nil instead of EOF, because Copy is defined as reading from src until EOF, and it does not treat reading to EOF as an error that should be reported.
If src implements WriterTo interface, this function will call (dst) for copying; otherwise, if dst implements ReaderFrom interface, this function will call (src) for copying.
- func CopyN
func CopyN(dst Writer, src Reader, n int64) (written int64, err error)
Copy n bytes of data from src to dst until EOF is reached on src or an error occurs.
Returns the number of copied bytes and the first error encountered.
Only when err is nil, writeten will equal n.
If dst implements ReaderFrom interface, this function calls it to copy.
- func ReadAtLeast
func ReadAtLeast(r Reader, buf []byte, min int) (n int, err error)
ReadAtLeast reads at least min byte data from r to fill it into buf.
The function returns the number of bytes written and the error (if not enough bytes are read).
EOF can only be returned if no bytes are read; if EOF is encountered when reading EOF that is not enough, the function will return ErrUnexpectedEOF.
If min is larger than buf's length, the function returns ErrShortBuffer.
Only when the return value err is nil, the return value n will not be less than min.
- func ReadFull
func ReadFull(r Reader, buf []byte) (n int, err error)
ReadFull accurately reads len(buf) byte data from r to fill it into buf.
The function returns the number of bytes written and the error (if not enough bytes are read).
EOF can only be returned if no bytes are read; if EOF is encountered when reading EOF that is not enough, the function will return ErrUnexpectedEOF.
Only when the return value err is nil, the return value n will be equal to len(buf).
- func WriteString
func WriteString(w Writer, s string) (n int, err error)
The WriteString function writes the content of the string s into w.
If w has implemented the WriteString method, the function will call the method directly.
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.