August 2019
Beginner to intermediate
798 pages
17h 2m
English
In this section, you will see a Go version of the cat(1) utility. You will most likely be surprised by the length of the program. The source code of cat.go will be presented in three parts. The first part of cat.go follows:
package main
import (
"bufio"
"fmt"
"io"
"os"
)
The second code segment of cat.go contains the following Go code:
func printFile(filename string) error {
f, err := os.Open(filename)
if err != nil {
return err
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
io.WriteString(os.Stdout, scanner.Text())
io.WriteString(os.Stdout, "\n")
}
return nil
}
In this part, you can see the implementation of a function whose purpose it is to print the contents of a file in the ...
Read now
Unlock full access