I have a text file with multi-line rows, delimited by a blank line. What would be the best way to read that row for row in Go?
I think I may have to use a Scanner with my own Split function, but just wondering if there is a better/easier way that I am missing.
I have tried using my own Splitfunc based on bufio.ScanLines:
func MyScanLines(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}
if i := bytes.IndexAny(data, "\n\n"); i >= 0 {
return i + 1, dropCR(data[0:i]), nil
}
if atEOF {
return len(data), dropCR(data), nil
}
return 0, nil, nil
}
But I get an error on the IndexAny call: "syntax error: unexpected semicolon or newline, expecting )" - Fixed that
Update: Fixed the syntax error above as suggested, but I only get the first line returned. I am reading the file as follows:
scanner.Split(MyScanLines)
scanner.Scan()
fmt.Println(scanner.Text())
Any suggestions?
Example of test file I am trying to read:
Name = "John"
Surname = "Smith"
Val1 = 700
Val2 = 800
Name = "Pete"
Surname = "Jones"
Val1 = 555
Val2 = 666
Val3 = 444
.
.
.