Create a gist now

Instantly share code, notes, and snippets.

@mchd075 /readCsv.go
Last active Oct 15, 2016

What would you like to do?
package main
import (
"bufio"
"encoding/csv"
"fmt"
"io"
"log"
"os"
)
func readCsv(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
// Create a new reader.
r := csv.NewReader(bufio.NewReader(file))
var values []string
for {
record, err := r.Read()
// Stop at EOF.
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
for _, value := range record {
values = append(values, value)
}
}
return values, err
}
func main() {
values, err := readCsv("---yourfilepath---")
if err != nil {
log.Fatalf("readCsv: %s", err)
}
fmt.Println(values)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment