@@ -90,9 +90,11 @@ func main() {
9090if * workers < 0 {
9191log .Fatalf ("-j must >= 0" )
9292}
93+
9394if * workers == 0 {
9495* workers = runtime .NumCPU ()
9596}
97+
9698if * debugFlag {
9799* workers = 1
98100}
@@ -152,13 +154,16 @@ func main() {
152154// Output logger
153155//
154156var cleanup func ()error
157+
155158output ,cleanup = newLogger (* quietFlag ,* outFlag )
159+
156160defer func () {_ = cleanup () }()
157161
158162//
159163// Custom output format
160164//
161165var err error
166+
162167defaultWrite ,defaultRead ,err = createTemplates (* format )
163168if err != nil {
164169log .Fatal (err )
@@ -176,14 +181,18 @@ func main() {
176181r .Compile ()
177182
178183args := flag .Args ()
184+
179185debug .Printf ("initialization complete in %v" ,time .Since (t ))
180186
181187// stdin/stdout
182188if len (args )== 0 {
183189// If we are working with pipes/stdin/stdout there is no concurrency,
184190// so we can directly send data to the writers.
185- var fileOut io.Writer
186- var errOut io.Writer
191+ var (
192+ fileOut io.Writer
193+ errOut io.Writer
194+ )
195+
187196switch * writeit {
188197case true :
189198// If we are writing the corrected stream,
@@ -250,12 +259,15 @@ func main() {
250259if err == nil && ! info .IsDir () {
251260c <- path
252261}
262+
253263return nil
254264})
255265}
266+
256267close (c )
257268
258269count := 0
270+
259271for range * workers {
260272changed := <- results
261273count += changed
@@ -273,6 +285,7 @@ func main() {
273285
274286func worker (writeit bool ,r * misspell.Replacer ,mode string ,files <- chan string ,results chan <- int ) {
275287count := 0
288+
276289for filename := range files {
277290orig ,err := misspell .ReadTextFile (filename )
278291if err != nil {
@@ -286,8 +299,10 @@ func worker(writeit bool, r *misspell.Replacer, mode string, files <-chan string
286299
287300debug .Printf ("Processing %s" ,filename )
288301
289- var updated string
290- var changes []misspell.Diff
302+ var (
303+ updated string
304+ changes []misspell.Diff
305+ )
291306
292307if mode == "go" {
293308updated ,changes = r .ReplaceGo (orig )
@@ -324,6 +339,7 @@ func worker(writeit bool, r *misspell.Replacer, mode string, files <-chan string
324339os .WriteFile (filename , []byte (updated ),0 )
325340}
326341}
342+
327343results <- count
328344}
329345
@@ -332,6 +348,7 @@ func readUserDict(userDictPath string) ([]string, error) {
332348if err != nil {
333349return nil ,fmt .Errorf ("failed to load user defined corrections %q: %w" ,userDictPath ,err )
334350}
351+
335352defer func () {_ = file .Close () }()
336353
337354reader := csv .NewReader (file )
@@ -365,11 +382,13 @@ func createTemplates(format string) (writeTmpl, readTmpl *template.Template, err
365382if err != nil {
366383return nil ,nil ,fmt .Errorf ("unable to compile log format: %w" ,err )
367384}
385+
368386return tmpl ,tmpl ,nil
369387
370388default :// format == ""
371389writeTmpl = template .Must (template .New ("defaultWrite" ).Parse (defaultWriteTmpl ))
372390readTmpl = template .Must (template .New ("defaultRead" ).Parse (defaultReadTmpl ))
391+
373392return
374393}
375394}
@@ -393,6 +412,7 @@ func newLogger(quiet bool, outputPath string) (logger *log.Logger, cleanup func(
393412if err != nil {
394413log .Fatalf ("unable to create outfile %q: %s" ,outputPath ,err )
395414}
415+
396416return log .New (fo ,"" ,0 ),fo .Close
397417}
398418