How many lines in the file?

Report a typo

Read the file sample.txt then use a for scanner.Scan() loop to iterate over each line of the file, count the total number of lines within the lineCount variable, and finally print the total amount of lines the file has.

Write a program
package main

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

func main() {
file, err := ? // open 'sample.txt' here with the os.Open function
if err != nil {
log.Fatal(err)
}
defer file.Close()

var lineCount int // lineCount will be used to count the number of lines in the file

scanner := bufio.NewScanner(?) // create a new scanner for the file

// Use the for scanner.Scan() loop to read the file line by line
// and increment the lineCount variable each loop
for ? {
?
}

fmt.Println(lineCount) // print the total lineCount here!
}
___

Create a free account to access the full topic