// Homework 31, problem 3: A run counter. // // The file runs.txt has one line per run so far. Write countLines, which // returns how many lines a file has. A missing file has 0 lines and is not an // error: use errors.Is with os.ErrNotExist. Then write appendLine, which adds // one line to the end of the file, creating it if needed. // // main is finished: each run prints "This is run N." and adds "Run N" to the // file. The file is created in the folder you run the program from. // // You will need to add "bufio" and "errors" to the imports. // // Run with: go run exercises/31/homework/03-run-counter.go package main import ( "fmt" ) func countLines(name string) (int, error) { // TODO: open the file and count its lines. A missing file means 0. return 0, nil } func appendLine(name string, line string) error { // TODO: open the file for appending, and write the line. fmt.Println("TODO: appendLine does not write yet") return nil } func main() { count, err := countLines("runs.txt") if err != nil { fmt.Println("Error:", err) return } run := count + 1 fmt.Printf("This is run %d.\n", run) err = appendLine("runs.txt", fmt.Sprintf("Run %d", run)) if err != nil { fmt.Println("Error:", err) } }