Skip to content

Instantly share code, notes, and snippets.

@mhshohel
Forked from drernie/csvtomap.go
Created August 1, 2020 20:26
Show Gist options
  • Save mhshohel/1a3d8db054c29aafae36eb0b099d4495 to your computer and use it in GitHub Desktop.
Save mhshohel/1a3d8db054c29aafae36eb0b099d4495 to your computer and use it in GitHub Desktop.

Revisions

  1. @drernie drernie created this gist Mar 10, 2017.
    25 changes: 25 additions & 0 deletions csvtomap.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    // 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)
    rows := []map[string]string{}
    var header []string
    for {
    record, err := r.Read()
    if err == io.EOF {
    break
    }
    if err != nil {
    log.Fatal(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
    }