# README
High level CSV library for Go 1.18+
This is a library for decoding and encoding CSV at high level as it provides convenient methods and many configuration options to process the data the way you expect.
This library is inspired by the project https://github.com/jszwec/csvutil.
Functionalities
Decoding
- Decode CSV data into Go struct
- Support Go interface
encoding.TextUnmarshaler
(with functionUnmarshalText
) - Support custom interface
CSVUnmarshaler
(with functionUnmarshalCSV
) - Ability to continue decoding when error occurs (collect all errors at once)
- Ability to perform custom preprocessor functions on cell data before decoding
- Ability to perform custom validator functions on cell data after decoding
- Ability to decode dynamic columns into Go struct field (inline columns)
- Support rendering the result errors into human-readable content (row-by-row text and CSV)
- Support localization to render the result errors into a specific language
Encoding
- Encode Go struct into CSV data
- Support Go interface
encoding.TextMarshaler
(with functionMarshalText
) - Support custom interface
CSVMarshaler
(with functionMarshalCSV
) - Ability to perform custom postprocessor functions on cell data after encoding
- Ability to encode dynamic columns defined via inner Go struct (inline columns)
- Ability to localize the header into a specific language
Installation
go get github.com/tiendc/go-csvlib
Usage
Benchmarks
csvlib vs csvutil vs gocsv vs easycsv
Unmarshal
BenchmarkUnmarshal/csvlib.Unmarshal/100_records
BenchmarkUnmarshal/csvlib.Unmarshal/100_records-10 21572 55520 ns/op
BenchmarkUnmarshal/csvlib.Unmarshal/1000_records
BenchmarkUnmarshal/csvlib.Unmarshal/1000_records-10 2641 455794 ns/op
BenchmarkUnmarshal/csvlib.Unmarshal/10000_records
BenchmarkUnmarshal/csvlib.Unmarshal/10000_records-10 253 4716323 ns/op
BenchmarkUnmarshal/csvlib.Unmarshal/100000_records
BenchmarkUnmarshal/csvlib.Unmarshal/100000_records-10 26 44519502 ns/op
BenchmarkUnmarshal/csvutil.Unmarshal/100_records
BenchmarkUnmarshal/csvutil.Unmarshal/100_records-10 27848 42927 ns/op
BenchmarkUnmarshal/csvutil.Unmarshal/1000_records
BenchmarkUnmarshal/csvutil.Unmarshal/1000_records-10 2952 405309 ns/op
BenchmarkUnmarshal/csvutil.Unmarshal/10000_records
BenchmarkUnmarshal/csvutil.Unmarshal/10000_records-10 296 4059881 ns/op
BenchmarkUnmarshal/csvutil.Unmarshal/100000_records
BenchmarkUnmarshal/csvutil.Unmarshal/100000_records-10 28 40531973 ns/op
BenchmarkUnmarshal/gocsv.Unmarshal/100_records
BenchmarkUnmarshal/gocsv.Unmarshal/100_records-10 9830 118919 ns/op
BenchmarkUnmarshal/gocsv.Unmarshal/1000_records
BenchmarkUnmarshal/gocsv.Unmarshal/1000_records-10 1022 1164278 ns/op
BenchmarkUnmarshal/gocsv.Unmarshal/10000_records
BenchmarkUnmarshal/gocsv.Unmarshal/10000_records-10 86 12609154 ns/op
BenchmarkUnmarshal/gocsv.Unmarshal/100000_records
BenchmarkUnmarshal/gocsv.Unmarshal/100000_records-10 9 119912333 ns/op
BenchmarkUnmarshal/easycsv.ReadAll/100_records
BenchmarkUnmarshal/easycsv.ReadAll/100_records-10 3831 315302 ns/op
BenchmarkUnmarshal/easycsv.ReadAll/1000_records
BenchmarkUnmarshal/easycsv.ReadAll/1000_records-10 384 3083931 ns/op
BenchmarkUnmarshal/easycsv.ReadAll/10000_records
BenchmarkUnmarshal/easycsv.ReadAll/10000_records-10 34 31440493 ns/op
BenchmarkUnmarshal/easycsv.ReadAll/100000_records
BenchmarkUnmarshal/easycsv.ReadAll/100000_records-10 4 324321531 ns/op
Marshal
BenchmarkMarshal/csvlib.Marshal/100_records
BenchmarkMarshal/csvlib.Marshal/100_records-10 19753 58890 ns/op
BenchmarkMarshal/csvlib.Marshal/1000_records
BenchmarkMarshal/csvlib.Marshal/1000_records-10 2149 554537 ns/op
BenchmarkMarshal/csvlib.Marshal/10000_records
BenchmarkMarshal/csvlib.Marshal/10000_records-10 214 5575920 ns/op
BenchmarkMarshal/csvlib.Marshal/100000_records
BenchmarkMarshal/csvlib.Marshal/100000_records-10 19 55735281 ns/op
BenchmarkMarshal/csvutil.Marshal/100_records
BenchmarkMarshal/csvutil.Marshal/100_records-10 24388 48931 ns/op
BenchmarkMarshal/csvutil.Marshal/1000_records
BenchmarkMarshal/csvutil.Marshal/1000_records-10 2557 467704 ns/op
BenchmarkMarshal/csvutil.Marshal/10000_records
BenchmarkMarshal/csvutil.Marshal/10000_records-10 256 4720885 ns/op
BenchmarkMarshal/csvutil.Marshal/100000_records
BenchmarkMarshal/csvutil.Marshal/100000_records-10 22 48627754 ns/op
BenchmarkMarshal/gocsv.Marshal/100_records
BenchmarkMarshal/gocsv.Marshal/100_records-10 13254 90873 ns/op
BenchmarkMarshal/gocsv.Marshal/1000_records
BenchmarkMarshal/gocsv.Marshal/1000_records-10 1294 898938 ns/op
BenchmarkMarshal/gocsv.Marshal/10000_records
BenchmarkMarshal/gocsv.Marshal/10000_records-10 132 9017481 ns/op
BenchmarkMarshal/gocsv.Marshal/100000_records
BenchmarkMarshal/gocsv.Marshal/100000_records-10 12 90260420 ns/op
Contributing
- You are welcome to make pull requests for new functions and bug fixes.
Authors
- Dao Cong Tien (tiendc)
License
# Functions
GetHeader get CSV header from the given struct.
GetHeaderDetails get CSV header details from the given struct type.
Marshal convenient method to encode a slice of structs into CSV format.
NewCellError creates a new CellError.
NewCSVRenderer creates a new CSVRenderer.
NewDecoder creates a new Decoder object.
NewEncoder creates a new Encoder object.
NewErrors creates a new Errors object.
NewRenderer creates a new SimpleRenderer.
NewRowErrors creates a new RowErrors.
ProcessorNumberGroupComma formats a number with grouping its digits by comma.
ProcessorNumberUngroupComma ungroups number digits by comma.
Unmarshal convenient method to decode CVS data into a slice of structs.
ValidatorGT validates a value to be greater than the given value.
ValidatorGTE validates a value to be greater than or equal to the given value.
ValidatorIN validates a value to be one of the specific values.
ValidatorLT validates a value to be less than the given value.
ValidatorLTE validates a value to be less than or equal to the given value.
ValidatorRange validates a value to be in the given range (min and max are inclusive).
ValidatorStrLen validates a string to have length in the given range.
ValidatorStrPrefix validates a string to have prefix matching the given one.
ValidatorStrSuffix validates a string to have suffix matching the given one.
# Constants
No description provided by the author
# Variables
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
ProcessorLower converts a string to lowercase.
ProcessorNumberGroup formats a number with grouping its digits.
ProcessorNumberUngroup ungroups number digits.
ProcessorReplace replaces a substring in a string.
ProcessorReplaceAll replaces all occurrences of a substring in a string.
ProcessorTrim trims space of string.
ProcessorTrimPrefix trims prefix from a string.
ProcessorTrimSuffix trims suffix from a string.
ProcessorUpper converts a string to uppercase.
# Structs
CellError data structure of error of a cell.
ColumnDetail details of a column parsed from a struct tag.
No description provided by the author
CSVRenderer an implementation of error renderer which can produce messages for the input errors as CSV output data.
DecodeColumnConfig configuration for decoding a specific column.
DecodeConfig configuration for decoding CSV data as structs.
Decoder data structure of the default decoder.
DecodeResult decoding result.
EncodeColumnConfig configuration for encoding a specific column.
EncodeConfig configuration for encoding Go structs as CSV data.
Encoder data structure of the default encoder.
No description provided by the author
Errors represents errors returned by the encoder or decoder.
InlineColumn represents inline columns of type `T`.
RowErrors data structure of error of a row.
SimpleRenderer a simple implementation of error renderer which can produce a text message for the input errors.
# Interfaces
CSVMarshaler marshaler interface for encoding custom type.
CSVUnmarshaler unmarshaler interface for decoding custom type.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Reader reader object interface required by the lib to read CSV data.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Writer writer object interface required by the lib to write CSV data to.
# Type aliases
DecodeFunc decode function for a given cell text.
DecodeOption function to modify decoding config.
EncodeFunc encode function for a given Go value.
EncodeOption function to modify encoding config.
LocalizationFunc function to translate message into a specific language.
OnCellErrorFunc function to be called when error happens on decoding cell value.
No description provided by the author
ProcessorFunc function to transform cell value before decoding or after encoding.
ValidatorFunc function to validate the values of decoded cells.