Documentation
¶
Overview ¶
Package csvlib provides readers (iterators) and writers for CSV files.
Index ¶
- Constants
- func NewDefaultIterator[T any](input io.Reader, hasHeader bool, conversionFunc ParseFunc[T]) (iter.Seq2[Record[T], error], error)
- func NewIterator[T any](input io.Reader, hasHeader bool, bufferSize int, conversionFunc ParseFunc[T]) (iter.Seq2[Record[T], error], error)
- type ConvertFunc
- type IterationError
- type ParseError
- type ParseFunc
- type Record
- type Writer
Examples ¶
Constants ¶
const DefaultBufferSize = 1024 * 4
DefaultBufferSize is the default buffer size used for reading records.
Variables ¶
This section is empty.
Functions ¶
func NewDefaultIterator ¶
func NewDefaultIterator[T any](input io.Reader, hasHeader bool, conversionFunc ParseFunc[T]) (iter.Seq2[Record[T], error], error)
NewDefaultIterator returns an iterator with a DefaultBufferSize.
Example ¶
// prepare sample data
const csvData = `"first_name","last_name"
"John","Doe"
"Jane","Doe"
`
input := strings.NewReader(csvData)
iter, err := NewDefaultIterator[ExampleRecord](input, true, csvToExampleRecord)
if err != nil {
fmt.Println("error: ", err.Error())
}
// process each record
for rec, err := range iter {
if err != nil {
fmt.Println("error: ", err.Error())
break
}
fmt.Printf("record: %d, first name: %q, last name: %q\n", rec.LineNumber, rec.Data.FirstName, rec.Data.LastName)
}
Output: record: 2, first name: "John", last name: "Doe" record: 3, first name: "Jane", last name: "Doe"
Types ¶
type ConvertFunc ¶
ConvertFunc converts type T to a []string record for CSV writing output.
type IterationError ¶
type IterationError struct {
// contains filtered or unexported fields
}
IterationError is returned when an error occurs during2 CSV file iteration.
func NewIterationError ¶
func NewIterationError(lineNumber int, cause error) *IterationError
func (*IterationError) Error ¶
func (ie *IterationError) Error() string
Error returns context specific IterationError information.
func (*IterationError) Unwrap ¶
func (ie *IterationError) Unwrap() error
Unwrap returns our inner error, aka "the cause".
type ParseError ¶
type ParseError struct {
// contains filtered or unexported fields
}
ParseError is returned when an error occurs parsing a raw []string record to type T.
func NewParseError ¶
func NewParseError(lineNumber int, cause error) *ParseError
NewParseError returns a ParseError with the specified context information.
func (*ParseError) Error ¶
func (ce *ParseError) Error() string
Error returns context specific ParseError information.
func (*ParseError) Unwrap ¶
func (ce *ParseError) Unwrap() error
Unwrap returns our inner error, aka "the cause".
type Record ¶
Record encapsulates a csv record including the record's line number and associated data.
type Writer ¶
type Writer[T any] struct { // contains filtered or unexported fields }
Writer writes csv records to an output file. convertFunc is used to convert type T to []string for CSV writer output.
func NewWriter ¶ added in v0.10.0
NewWriter creates a new Writer which writes records of type T to an output target.
Example ¶
var output bytes.Buffer
writer, err := NewWriter(&output, exampleRecordToCsv)
if err != nil {
fmt.Println("error creating writer ", err.Error())
return
}
// writer will flush prior to close
defer writer.Close()
err = writer.WriteHeader([]string{"first_name", "last_name"})
if err != nil {
fmt.Println("error writing header: ", err.Error())
return
}
for _, rec := range []ExampleRecord{{"John", "Doe"}, {"Jane", "Doe"}} {
if err := writer.Write(rec); err != nil {
fmt.Println("error writing record: ", err.Error())
break
}
}
// flush our output so that it will be available to print
writer.Flush()
fmt.Println(output.String())
Output: first_name,last_name John,Doe Jane,Doe
func (*Writer[T]) WriteHeader ¶
WriteHeader writes a header to the output csv file.