file, err := os.Open("sample.txt") if err != nil { fmt.Println("Error opening file:", err) return } defer file.Close() scanner := bufio.NewScanner(file) for scanner.Scan() { fmt.Println(scanner.Text()) } if scanner.Err() != nil { fmt.Println("Error reading file:", scanner.Err()) }In this example, we first open the file "sample.txt" and check for any errors using the os package. If the file opens successfully, we create a new scanner object using bufio.NewScanner and loop through each line in the file using scanner.Scan(). If an error occurs during scanning, the program will print an error message using the scanner.Err() method. Overall, the bufio package provides a convenient way to read data for various sources in Go, and the Err method helps to handle errors that may occur during scanning.