Map CSV Rows into Typed Go Structs
Decode CSV rows into typed structs with header checks, per-line error context, and rejection of negative numeric fields.
Code
package main
import (
"encoding/csv"
"fmt"
"io"
"strconv"
"strings"
)
type Record struct {
SKU string
Quantity int
Price int
}
var header = []string{"sku", "quantity", "price_cents"}
func parseRecords(input string) ([]Record, error) {
reader := csv.NewReader(strings.NewReader(input))
reader.TrimLeadingSpace = true
first, err := reader.Read()
if err != nil {
return nil, fmt.Errorf("read header: %w", err)
}
if len(first) != len(header) {
return nil, fmt.Errorf("header has %d columns, want %d", len(first), len(header))
}
for index, name := range header {
if first[index] != name {
return nil, fmt.Errorf("column %d: got %q want %q", index, first[index], name)
}
}
var records []Record
for line := 2; ; line++ {
row, err := reader.Read()
if err == io.EOF {
break
}
if err != nil {
return nil, fmt.Errorf("line %d: %w", line, err)
}
quantity, err := strconv.Atoi(row[1])
if err != nil || quantity < 0 {
return nil, fmt.Errorf("line %d: invalid quantity %q", line, row[1])
}
price, err := strconv.Atoi(row[2])
if err != nil || price < 0 {
return nil, fmt.Errorf("line %d: invalid price %q", line, row[2])
}
records = append(records, Record{SKU: row[0], Quantity: quantity, Price: price})
}
return records, nil
}
func main() {
input := "sku,quantity,price_cents\nA-1,5,1200\nB-2,3,800\n"
records, err := parseRecords(input)
if err != nil {
panic(err)
}
for _, record := range records {
fmt.Printf("%s qty=%d price=%d\n", record.SKU, record.Quantity, record.Price)
}
if len(records) != 2 || records[0].SKU != "A-1" || records[0].Price != 1200 {
panic("unexpected records")
}
if _, err := parseRecords("sku,quantity,price_cents\nA-1,-2,100\n"); err == nil {
panic("negative quantity should fail")
}
}
How it works
All of the logic lives in the functions parseRecords and main.
Iteration happens inside for index, name := range header — everything outside it runs once. Error handling is inline: 5 places test err != nil and branch immediately.
What to watch for
- It is dependency-free in the strict sense — the import list, if any, is empty of packages.
- There are no exceptions to catch —
errreturn values must be checked at each call site.
Putting it to use
The definition that matters is func parseRecords(input string) ([]Record, error)
Running the file executes main, which drives parseRecords in order.
The core lines, annotated
return nil, fmt.Errorf("line %d: %w", line, err)
return nil, fmt.Errorf("read header: %w", err)
return nil, fmt.Errorf("header has %d columns, want %d", len(first), len(header))
Interactive Tools for This Topic
CSV Column AnalyzerProfile every CSV column with inferred types, empty-cell counts, unique values, and numeric min and max ranges.CSV to JSON ConverterConvert comma-separated values into a JSON array of objects using the first row as field names, with support for quoted and escaped fields.CSV to Markdown TableConvert CSV data, including quoted fields with embedded commas and newlines, into a clean GitHub-flavored Markdown table with selectable alignment.CSV to TSV ConverterParse RFC 4180 CSV with quoted fields and re-serialize using a tab, pipe, semicolon, or custom delimiter, with optional quoting.
FAQ
- How do errors reach the caller?
- The Go way: `err` comes back from each call, and 5 checkpoints here test it before proceeding.
- How do you run Map CSV Rows into Typed Go Structs?
- It runs as-is — treat the file as the whole program and execute it.
Report a problem with this exampleOpens a pre-filled issue on GitHub.