Skip to content

Instantly share code, notes, and snippets.

@broady
Created November 20, 2017 20:38
Show Gist options
  • Select an option

  • Save broady/25eca1315b4bdc77c0eb76bd7bc0d1b1 to your computer and use it in GitHub Desktop.

Select an option

Save broady/25eca1315b4bdc77c0eb76bd7bc0d1b1 to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"io/ioutil"
"log"
"os"
"path/filepath"
"time"
"flag"
"sync"
)
type tracker struct {
files int
lines int
mu *sync.Mutex
}
func main() {
path := flag.String("path", ".", "Please provide a path")
flag.Parse()
wg := sync.WaitGroup{}
tracker := tracker{mu: &sync.Mutex{}}
t := time.NewTicker(1)
go func() {
for {
<-t.C
tracker.mu.Lock()
log.Printf("progress: files=%s; lines=%s\n", tracker.files, tracker.lines)
tracker.mu.Unlock()
}
}()
filepath.Walk(*path, func(path string, info os.FileInfo, err error) error {
go func() {
wg.Add(1)
defer wg.Done()
tracker.Lock()
defer tracker.Unlock()
tracker.files++
file, err := ioutil.ReadFile(path)
if err != nil {
panic(err)
}
tracker.num_lines += bytes.Count(file, []byte("\n"))
return nil
}()
})
wg.Wait()
t.Stop()
log.Printf("files=%s; lines=%s\n", tracker.files, tracker.lines)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment