Home
Map
csv ExamplesUse the encoding-cvs package. Call NewReader and Read to read records.
Go
This page was last reviewed on Dec 8, 2023.
CSV. Go programs can handle CSV. Comma-separated values files are a form of flat databases. They can store small amounts of information in an efficient way.
File
With Go and the "encoding/csv" package, we can read lines from CSV files. We can invoke, from the "os" package, a method like os.Open to specify a file.
First example. 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).
Then We create a new reader with bufio and pass it to the csv.NewReader method. We use Read() and test EOF.
Note We display the entire record with Println. Then we use len to determine the number of values in each record.
Note 2 We use range to iterate over the indexes of the record slice. We access individual records from the line.
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
ReadAll, strings. We can read lines from a string. First we must use strings.NewReader and use the string as the argument. We pass that Reader to csv.NewReader.
Start ReadAll consumes the entire CSV Reader's data at once. We then can use a for-loop to iterate over the lines.
for
Here We ignore the error result from ReadAll. We use an underscore variable name to discard the error.
package main import ( "encoding/csv" "fmt" "strings" ) func main() { // Create a 2-line string. data := " fish,blue,water \n fox,red,farm " // 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]) } }
Lines: 2 Elements: 3 [ fish blue water ] Elements: 3 [ fox red farm ]
2D slice. With ReadAll we receive a 2D slice of lines and the values within each line. We can use len to count elements in a line. With append() we can add to this 2D slice.
len
2D Slice
Advantages. Why not just use a Scanner and Split each line in a file? The csv package can help us avoid some code. We can reuse the code provided in the Go standard library.
A review. The "encoding/csv" package is powerful. We set options on the Reader to handle different formats of files. We read CSV values from a file on the disk.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Dec 8, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.