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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.