Parsing a text file into a struct in Go

Report a typo
Hey there! This problem might be a bit unpredictable, but give it a go and let us know how you do!
In the Go language, suppose we have a structured text file with information we want to parse into a custom data structure. Which of the following methods could be used to efficiently read and parse this file given that you've defined a 'parseLine' function that takes in a line of text and returns a struct? Consider the following code:
type Data struct {
  Field1 string
  Field2 int
}

func parseLine(line string) Data {
  // Implementation left as an exercise...
}

func main() {
  file, err := os.Open('data.txt')
  if err != nil {
    log.Fatal(err)
  }
  defer file.Close()

  var data []Data

  // What code goes here to fill 'data'?
}
Select one option from the list

Create a free account to access the full topic