Skip to content

Instantly share code, notes, and snippets.

@djale1k
Created August 25, 2021 11:01
CSV File To Map
// CSVToMap takes a reader and returns an array of dictionaries, using the header row as the keys
func CSVToMap(reader io.Reader) []map[string]string {
r := csv.NewReader(reader)
var rows []map[string]string
var header []string
for {
record, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
fmt.Println(err)
}
if header == nil {
header = record
} else {
dict := map[string]string{}
for i := range header {
dict[header[i]] = record[i]
}
rows = append(rows, dict)
}
}
return rows
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment