September 2017
Intermediate to advanced
466 pages
9h 33m
English
The io package offers functions that allow you to write to or read from files. Its use will be illustrated in the usingIO.go file, which will be presented in three parts. What the program does is read 8 bytes from a file and write them in a standard output.
The first part is the preamble of the Go program:
package main import ( "fmt" "io" "os" )
The second part is the following Go code:
func main() {
if len(os.Args) != 2 {
fmt.Println("Please provide a filename")
os.Exit(1)
}
filename := os.Args[1]
f, err := os.Open(filename)
if err != nil {
fmt.Printf("error opening %s: %s", filename, err)
os.Exit(1)
}
defer f.Close()
The program also uses the handy defer command that defers the execution of a function until the surrounding ...
Read now
Unlock full access