func (*File) Read
func (f *File) Read(b []byte) (n int, err error)
// Read reads up to len(b) bytes from the File.
// It returns the number of bytes read and any error encountered.
// At end of file, Read returns 0, io.EOF.
func (f *File) Read(b []byte) (n int, err error) {
if err := f.checkValid("read"); err != nil {
return 0, err
}
n, e := f.read(b)
return n, f.wrapErr("read", e)
}
Read method, one take []byte slice argument. Return two values namely n int type and err error type.
In this example, an assumption that the file already opens with os.Open(). a make initializes the variable on a slice of []byte type with has a quantity of 100 of memory of bytes size (uint8 – unsigned 8-bit integers).
data := make([]byte, 100)
count, err := file.Read(data)
if err != nil {
log.Fatal(err)
}
fmt.Printf("read %d bytes: %q\n", count, data[:count])
file.Read(data) read a stream of 100 bytes into the count variable contains the number in int type and err variable contains the error value
Note: file contents may have CR character, will be counted