A better version of wc.go

As we talked about in Chapter 6, File Input and Output, in this chapter, you will learn how to create a version of wc.go that uses goroutines. The name of the new utility will be dWC.go and will be presented in four parts. Note that the current version of dWC.go considers each command-line argument as a file.

The first part of the utility is the following:

package main 
 
import ( 
   "bufio" 
   "fmt" 
   "io" 
   "os" 
   "path/filepath" 
   "regexp" 
   "sync" 
) 

The second part has the following Go code:

func count(filename string) { var err error var numberOfLines int = 0 var numberOfCharacters int = 0 var numberOfWords int = 0 f, err := os.Open(filename) if err != nil { fmt.Printf("%s\n", err) return } defer f.Close() r := bufio.NewReader(f) ...

Get Go Systems Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.