Reading numbers from a file

Report a typo

Emma has just received a file prime_numbers.txt that has the first 100 prime numbers:

2
3
5
7
11
...

This is the code she has written so far:

package main

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

func main () {
    file, err := os.Open("prime_numbers.txt")
    if err != nil {
        log.Panic(err)
    }
    defer file.Close()

    scanner := bufio.NewScanner(file)

    // The selected code snippet will be added here
}

Can you help her by selecting among all the code snippets below those that will make her program print the first 10 prime numbers?

a)

    for scanner.Scan() {
        fmt.Println(scanner.Text())
    }

b)

    for i := 0; i < 10; i++ {
        scanner.Scan()
        fmt.Println(scanner.Text())
    }

c)

    for i := 0; scanner.Scan() && i < 10; i++ {
        fmt.Println(scanner.Text())
    }

d)

    scanner.Split(bufio.ScanWords)
    fmt.Println(scanner.Text())
Select one or more options from the list
___

Create a free account to access the full topic