Skip to content

Instantly share code, notes, and snippets.

@clairmont32
Created December 12, 2020 19:38
Show Gist options
  • Save clairmont32/dfed72a7478d785a97943c7cec5decac to your computer and use it in GitHub Desktop.
Save clairmont32/dfed72a7478d785a97943c7cec5decac to your computer and use it in GitHub Desktop.
Concurrent File Reader
// walk a given dir and concurrently read all `go` files found.
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"sync"
"time"
)
// walk through the directory and return a []string of file paths
func enumerateDir(dir string) (files []string) {
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() && strings.HasSuffix(info.Name(), "go") {
files = append(files, path)
}
return nil
})
if err != nil {
fmt.Println(err)
}
return files
}
func readFile(file string) {
fileBytes, err := ioutil.ReadFile(file)
if err != nil {
fmt.Printf("unable to read %s\n", file)
fmt.Println(err)
}
fmt.Println(string(fileBytes))
// or, ideally, do other stuff. #justagist
}
func parseFiles(fileList []string) {
// concurrently read files
var wg sync.WaitGroup
length := len(fileList)
wg.Add(length)
for _, file := range fileList {
go func(file string, length int) {
readFile(file)
}(file, length)
wg.Done()
}
wg.Wait()
}
func main() {
fileList := enumerateDir(".")
parseFiles(fileList)
time.Sleep(1 * time.Millisecond)
fmt.Println(fileList)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment