September 2017
Intermediate to advanced
466 pages
9h 33m
English
After this little introduction, it is time to see the Go code of wc.go, which will be presented in five parts. The first part is the expected preamble:
package main import ( "bufio" "flag" "fmt" "io" "os" "regexp" )
The second part is the implementation of the countLines() function, which includes the core functionality of the program. Note that the name countLines() may have been a poor choice as countLines() also counts the words and the characters of a file:
func countLines(filename string) (int, int, int) {
var err error
var numberOfLines int
var numberOfCharacters int
var numberOfWords int
numberOfLines = 0 numberOfCharacters = 0 numberOfWords = 0 f, err := os.Open(filename) if err != nil { fmt.Printf("error opening ...Read now
Unlock full access