The dWC.go utility revisited
In this section, we will change the implementation of the dWC.go
utility developed in the previous chapter.
The first version of the program will use a buffered channel whereas the second version of the program will use shared memory for keeping the counts for each file you process.
Using a buffered channel
The name of this implementation will be WCbuffered.go
and will be presented in five parts.
The first part of the utility is the following:
package main import ( "bufio" "fmt" "io" "os" "path/filepath" "regexp" ) type File struct { Filename string Lines int Words int Characters int Error error }
The File
structure will keep the counts for each input file. The second chunk of WCbuffered.go
has the following Go code:
func monitor(values <-chan File, count int) { var totalWords int = 0 var totalLines int = 0 var totalChars int = 0 for i := 0; i < count; i++ { x := <...