// Homework 36, challenge: Line counter. // // countLines is provided. Count the lines of every file in files at the // same time, one goroutine per file. Give each goroutine its own slot in a // slice of results, so that you can print the files in their original order // once all goroutines are finished. A file that cannot be read prints its // error in place of a count. Finish with the total of the lines counted. // // Run with: go run exercises/36/homework/04-line-counter.go package main import ( "fmt" "os" "strings" ) // countLines returns how many lines the file has. func countLines(path string) (int, error) { data, err := os.ReadFile(path) if err != nil { return 0, err } return strings.Count(string(data), "\n"), nil } // result holds the answer for one file. type result struct { lines int err error } func main() { files := []string{ "exercises/36/01-one-after-another.go", "exercises/36/04-waitgroup.go", "exercises/36/07-one-slot-each.go", "exercises/36/missing.go", } // TODO: write your code here, in place of these placeholder lines: fmt.Println("TODO: count the lines of", len(files), "files") _ = result{} }