C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Then: We create a new reader with bufio and pass it to the csv.NewReader method. We use Read() and test EOF.
Record: We display the entire record with Println. Then we use len to determine the number of values in each record.
Range: We use range to iterate over the indexes of the record slice. We access individual records from the line.
Golang program that uses csv, NewReader on file
package main
import (
"bufio"
"encoding/csv"
"os"
"fmt"
"io"
)
func main() {
// Load a TXT file.
f, _ := os.Open("C:\\programs\\file.txt")
// Create a new reader.
r := csv.NewReader(bufio.NewReader(f))
for {
record, err := r.Read()
// Stop at EOF.
if err == io.EOF {
break
}
// Display record.
// ... Display record length.
// ... Display all individual elements of the slice.
fmt.Println(record)
fmt.Println(len(record))
for value := range record {
fmt.Printf(" %v\n", record[value])
}
}
}
Contents: file.txt
cat,dog,bird
10,20,30,40
fish,dog,snake
Output
[cat dog bird]
3
cat
dog
bird
[10 20 30 40]
4
10
20
30
40
[fish dog snake]
3
fish
dog
snake
ReadAll: This consumes the entire CSV Reader's data at once. We then can use a for-loop to iterate over the lines.
ForUnderscore: In this example we ignore the error result from ReadAll. We use an underscore variable name to discard the error.
Raw literal: We specify the string as a raw literal with backtick characters. The string has three lines.
StringsGolang program that uses ReadAll, strings.NewReader
package main
import (
"encoding/csv"
"fmt"
"strings"
)
func main() {
// Create a 3-line string.
data := `fish,blue,water
fox,red,farm
sheep,white,mountain
frog,green,pond`
// Use strings.NewReader.
// ... This creates a new Reader for passing to csv.NewReader.
r := csv.NewReader(strings.NewReader(data))
// Read all records.
result, _ := r.ReadAll()
fmt.Printf("Lines: %v", len(result))
fmt.Println()
for i := range result {
// Element count.
fmt.Printf("Elements: %v", len(result[i]))
fmt.Println()
// Elements.
fmt.Println(result[i])
}
}
Output
Lines: 4
Elements: 3
[fish blue water]
Elements: 3
[fox red farm]
Elements: 3
[sheep white mountain]
Elements: 3
[frog green pond]