// Homework 31, problem 2: The longest line. // // Write longestLine, which opens the file, reads it one line at a time with a // bufio.Scanner, and returns the number and the text of the longest line. Close // the file with defer, and check scanner.Err() at the end. // // You will need to add "bufio" to the imports. // // Run with: go run exercises/31/homework/02-longest-line.go package main import ( "fmt" "os" ) func longestLine(name string) (int, string, error) { f, err := os.Open(name) if err != nil { return 0, "", err } // TODO: close the file with defer, then scan it line by line. f.Close() return 0, "", nil } func main() { number, text, err := longestLine("exercises/31/poem.txt") if err != nil { fmt.Println("Error:", err) return } fmt.Printf("Line %d is the longest (%d bytes):\n", number, len(text)) fmt.Println(text) }