Solution: implement wc
TODO: finish this
examples/wc/wc.go
package main import ( "bufio" "fmt" "io" "os" "strings" ) func main() { fmt.Println(os.Args) wc(os.Args[1:]) } func wc(filenames []string) (int, int, int) { rows := 0 words := 0 chars := 0 filename := filenames[0] fh, err := os.Open(filename) if err != nil { os.Exit(1) } reader := bufio.NewReader(fh) for { line, err := reader.ReadString('\n') if err != nil { if err != io.EOF { fmt.Println(err) } break } rows++ words += len(strings.Fields(line)) chars += len(line) } return rows, words, chars }
examples/wc/wc_test.go
package main import ( "testing" ) // var expected map[string] func TestWCa(t *testing.T) { files := []string{"one.txt"} rows, words, chars := wc(files) if rows != 2 { t.Errorf("Expected rows: 2, actual rows %v\n", rows) } if words != 5 { t.Errorf("Expected words: 5, actual words %v\n", words) } if chars != 24 { t.Errorf("Expected chars: 24, actual chars %v\n", chars) } } func TestWCb(t *testing.T) { files := []string{"two.txt"} rows, words, chars := wc(files) if rows != 3 { t.Errorf("Expected rows: 3, actual rows %v\n", rows) } if words != 11 { t.Errorf("Expected words: 11, actual words %v\n", words) } if chars != 100 { t.Errorf("Expected chars: 100, actual chars %v\n", chars) } }