Reading movie quotes from a file

Report a typo

Samuel has just finished writing one of his favorite movie quotes in a file called hamburger.txt — Now, he wants to create a Go program that opens and reads this file's contents in chunks and finally prints the read contents to the terminal.

Samuel has already written some code but forgot how to create a buffer and how to use the Read() function to read the file in chunks. Can you help him write the additional required code to achieve the above tasks?

Below is the expected output of the program if correctly reading the file in chunks:

Mm-hmm! This is a tasty burger!
Vincent! You ever had a Big Kahu
na burger?
Write a program
package main

import (
"errors"
"fmt"
"io"
"log"
"os"
)

const chunkSize = 32 // DO NOT modify the value of the chunk size!

func main() {
file, err := os.Open("hamburger.txt")
if err != nil {
log.Fatal(err)
}

// Create a slice of bytes buffer using `chunkSize`
buf := make(?, ?)

for {
// Use the `Read` function to read the `buf` slice of bytes
readTotal, err := file.?(?)
if err != nil {
file.Close()
if errors.Is(err, io.EOF) {
break
}
log.Fatal(err)
}
// Print the contents of the buffer that was read from the file
// up to the `readTotal` number of bytes read:
fmt.Println(string(buf[:?]))
}
}

___

Create a free account to access the full topic