January 2018
Intermediate to advanced
340 pages
8h 6m
English
The os.File type comes with a couple of basic functions. One of them is File.Read(). Read(), which expects a byte slice to be passed as a parameter. Bytes are read from the file and placed in the byte slice. Read() will read as many bytes as it can or until the buffer fills up, and then it will stop reading.
Multiple calls to Read() may be necessary before getting to the end of a file, depending on the size of the buffer provided and the size of the file. An io.EOF error is returned if the end of a file is reached during a call to Read():
package main import ( "log" "os" ) func main() { // Open file for reading file, err := os.Open("test.txt") if err != nil { log.Fatal(err) } defer file.Close() // Read up ...Read now
Unlock full access