August 2019
Beginner to intermediate
798 pages
17h 2m
English
The technique presented in this subsection will be demonstrated by the byWord.go file and shown in four parts. As you will see in the Go code, separating the words of a line can be tricky.
The first part of this utility is as follows:
package main
import (
"bufio"
"flag"
"fmt"
"io"
"os"
"regexp"
)
The second code portion of byWord.go is shown in the following Go code:
func wordByWord(file string) error {
var err error
f, err := os.Open(file)
if err != nil {
return err
}
defer f.Close()
r := bufio.NewReader(f)
for {
line, err := r.ReadString('\n')
if err == io.EOF {
break
} else if err != nil {
fmt.Printf("error reading file %s", err)
return err
}
This part of the wordByWord() function is the same as the ...
Read now
Unlock full access