When we operate io on files, we often see that we need to pass a or object as a parameter for reading and writing. So how should we or these RW objects? Actually, it is very simple. You only need to find out which objects implement the Read or Writer method. Then you only need to create an object that implements one of these two methods, and it can be a or .
Of course, the most common one should be our object. In addition, , etc. can be used as RW parameters for io.
Of course, you can also define an object yourself, implement the methods defined in the interface, and then your object can also be used as an RW entry parameter. This is the perfect embodiment of interface-oriented programming in the Go language.
Reader Writer interface definition in go
type Reader interface { Read(p []byte) (n int, err error) } type Writer interface { Write(p []byte) (n int, err error) }
RW implementation code in object
// Read reads up to len(b) bytes from the File and stores them in b. // It returns the number of bytes read and any error encountered. // At end of file, Read returns 0, . func (f *File) Read(b []byte) (n int, err error) { if err := ("read"); err != nil { return 0, err } n, e := (b) return n, ("read", e) } // Write writes len(b) bytes from b to the File. // It returns the number of bytes written and an error, if any. // Write returns a non-nil error when n != len(b). func (f *File) Write(b []byte) (n int, err error) { if err := ("write"); err != nil { return 0, err } n, e := (b) if n < 0 { n = 0 } if n != len(b) { err = } epipecheck(f, e) if e != nil { err = ("write", e) } return n, err }
RW implementation code in
// Read reads data into p. // It returns the number of bytes read into p. // The bytes are taken from at most one Read on the underlying Reader, // hence n may be less than len(p). // To read exactly len(p) bytes, use (b, p). // If the underlying Reader can return a non-zero count with , // then this Read method can do so as well; see the [] docs. func (b *Reader) Read(p []byte) (n int, err error) { n = len(p) if n == 0 { if () > 0 { return 0, nil } return 0, () } if == { if != nil { return 0, () } if len(p) >= len() { // Large read, empty buffer. // Read directly into p to avoid copy. n, = (p) if n < 0 { panic(errNegativeRead) } if n > 0 { = int(p[n-1]) = -1 } return n, () } // One read. // Do not use , which will loop. = 0 = 0 n, = () if n < 0 { panic(errNegativeRead) } if n == 0 { return 0, () } += n } // copy as much as we can // Note: if the slice panics here, it is probably because // the underlying reader returned a bad count. See issue 49795. n = copy(p, [:]) += n = int([-1]) = -1 return n, nil } // writeBuf writes the Reader's buffer to the writer. func (b *Reader) writeBuf(w ) (int64, error) { n, err := ([:]) if n < 0 { panic(errNegativeWrite) } += n return int64(n), err }
RW implementation code in
// Read reads the next len(p) bytes from the buffer or until the buffer // is drained. The return value n is the number of bytes read. If the // buffer has no data to return, err is (unless len(p) is zero); // otherwise it is nil. func (b *Buffer) Read(p []byte) (n int, err error) { = opInvalid if () { // Buffer is empty, reset to recover space. () if len(p) == 0 { return 0, nil } return 0, } n = copy(p, [:]) += n if n > 0 { = opRead } return n, nil } // Write appends the contents of p to the buffer, growing the buffer as // needed. The return value n is the length of p; err is always nil. If the // buffer becomes too large, Write will panic with ErrTooLarge. func (b *Buffer) Write(p []byte) (n int, err error) { = opInvalid m, ok := (len(p)) if !ok { m = (len(p)) } return copy([m:], p), nil }
Note that these methods are generally bound to pointer-type objects, so when you create the RW object you need, you need to use the & pointer symbol or use the new function to create the object, such as: w := &{} is equivalent to w := new()
This is the article about how to obtain 。 and 。 in io operations in go language. For more related go content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!