Here we open a file on the disk with os.Open. You will need to change the path to a CSV file that exists (the extension is not important).
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])
}
}
}
cat,dog,bird
10,20,30,40
fish,dog,snake
[cat dog bird]
3
cat
dog
bird
[10 20 30 40]
4
10
20
30
40
[fish dog snake]
3
fish
dog
snake