Favorite songs

Report a typo

Bruno has a file called songs.txt with all his favorite songs. He wants to open the file in his Go program to read the data from it and then print it to the console.

Bruno has already written the required code to print the file contents to the console; however, he forgot how to properly open the file! Please help Bruno write the additional required code to open songs.txt and store it within the file variable. Remember to implement error handling within the if statement.

This problem uses the bufio package to scan the text contained within songs.txt. To solve this task, you don't need to know how the bufio package works. The only objective is to open songs.txt and implement error handling within the if statement.

Write a program
package main

import (
"bufio"
"fmt"
"log"
"os"
)

func main() {
// Write the code below to open "songs.txt" and store it in the 'file' variable:
?, ? := ?("?")

// Implement error handling below:
if ? != ? {
log.Fatal(err) // exit if we have an error
}

// DO NOT delete this code block - it outputs the contents of the file after opening it
scanner := bufio.NewScanner(file)
for scanner.Scan() {
fmt.Println(scanner.Text())
}
file.Close() // always remember to close the file!
}

___

Create a free account to access the full topic