All contributions

gocarina/gocsv

gocsv

A Go library that turns CSV files into structs and structs back into CSV, driven by field tags.

Language
Go
GitHub stars
2,203
Merged changes
4

What we changed

Zero-padded numbers are read in base 10, SetHeaderNormalizer can be called during decoding without a data race, MarshalBytesWithoutHeaders was added, and the README example writes dates in the right layout.

What this work shows

  • Data races and sync/atomic in Go
  • Number parsing with strconv
  • Date layouts in Go's time package
What was broken

A leading zero turned a number into octal

toInt and toUint passed the field to strconv with base 0, so a leading zero meant octal. A field of 08 or 09 failed to parse, and 010 silently became 8.

How we fixed it

Integer fields read in base 10

Both calls now use base 10, so 010 is 10 and 08 is 8. Go-style prefixed values such as 0x1f now return a parse error. Tests cover negative numbers and decoding through UnmarshalString.

#293

What was broken

Changing the normaliser during decoding

SetHeaderNormalizer replaced the struct info cache while other goroutines were still using it. That ended in a panic or a failure under go test -race.

How we fixed it

Normaliser and cache swapped together

The normaliser and the struct infos built with it now live in one value that is swapped atomically with sync/atomic. A test calls SetHeaderNormalizer alongside UnmarshalString in 50 pairs of goroutines.

#303

What was broken

No MarshalBytes variant without headers

The library had MarshalStringWithoutHeaders but no counterpart that returns []byte. An issue in the project asked for exactly that function.

How we fixed it

A new MarshalBytesWithoutHeaders function

We added MarshalBytesWithoutHeaders, which writes the rows without a header line and returns bytes, like MarshalBytes. A test checks the exact output.

#305

What was broken

The example wrote dates as year, day, month

The layout 20060201 in Go's time package means year, day and month, so the README example wrote 4 November as 20220411.

How we fixed it

The right date layout in the examples

We changed the layout to 20060102 in the README and the full example, and the expected output in the example test now shows 20221104.

#304

Merged changes 4

  1. #305 Add MarshalBytesWithoutHeaders Merged 12 October 2026
  2. #304 Fix date layout in README and full example Merged 12 October 2026
  3. #303 Make SetHeaderNormalizer safe to call while decoding Merged 12 October 2026
  4. #293 Parse integer fields as base 10 (fixes #182) Merged 5 September 2026

What it taught us

A shared cache is a typical place for a data race: an atomic swap removes it, and a test that passes under go test -race guards it. We read spreadsheet data as data, not as Go literals: 010 means ten, not eight.

More about this technology: Golang

Other projects we write code for

Describe your project.

A free quote comes back within an hour during working hours.