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.
Reading files
How many lines in the file?
Report a typo
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!
}
___
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.
Create a free account to access the full topic
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.