-
-
Save broady/25eca1315b4bdc77c0eb76bd7bc0d1b1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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