Documentation
¶
Overview ¶
Package tocsv provides tools for converting various data sources to CSV format. It supports SQL database results, slices, and custom data sources through a unified RowsScanner interface with extensive customization options.
Index ¶
- func New(rs RowsScanner, opts ...Option) *csvWriter
- type Column
- type Option
- func WithCRLF(useCRLF bool) Option
- func WithCallback(fn func(row []string) ([]string, bool)) Option
- func WithCustomDelimiter(delimiter rune) Option
- func WithCustomHeader(customHeader []string) Option
- func WithCustomNULL(nullValue string) Option
- func WithCustomType[T any](fn func(v T, driver string, column Column) string) Option
- func WithHeader(writeHeader bool) Option
- type RowsScanner
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func New ¶
func New(rs RowsScanner, opts ...Option) *csvWriter
New creates a new csvWriter instance with the given RowsScanner and options.
Example:
writer := tocsv.New(rows,
tocsv.WithCustomDelimiter(';'),
tocsv.WithHeader(false),
)
Types ¶
type Column ¶
type Column struct {
Name string // The name of the column
Type string // The type of the column (database type name or Go type)
}
Column represents metadata about a single column in the data source. It contains the column name and its type information.
type Option ¶
type Option func(*csvWriter)
Option represents a functional option for configuring csvWriter behavior.
func WithCallback ¶
WithCallback sets a row processing callback function. The callback can modify or filter rows before writing. Return modified row and true to write, or false to skip.
func WithCustomDelimiter ¶
WithCustomDelimiter sets a custom field delimiter (default is comma).
func WithCustomHeader ¶
WithCustomHeader sets custom column headers (must match column count).
func WithCustomNULL ¶
WithCustomNULL sets the string representation for NULL values (default is empty).
func WithCustomType ¶
WithCustomType registers a custom conversion function for type T. The provided function will be called to convert values of type T to strings.
func WithHeader ¶
WithHeader configures whether to write column headers (default is true).
type RowsScanner ¶
type RowsScanner interface {
// Next prepares the next result row for reading.
// Returns true on success, false if no more rows are available.
Next() bool
// Scan copies the current row's values into the provided destinations.
// Each destination must be a pointer to an any value (*any).
Scan(dest ...any) error
// Columns returns the column metadata for the current result set.
Columns() ([]Column, error)
// Driver returns an identifier for the data source driver.
Driver() string
// Err returns any error that occurred during iteration.
Err() error
}
RowsScanner is an interface that abstracts data source scanning for CSV conversion. It mimics sql.Rows behavior but works with any tabular data source.
func NewSQLRowsScanner ¶
func NewSQLRowsScanner(rows *sql.Rows, driver string) RowsScanner
NewSQLRowsScanner creates a RowsScanner from sql.Rows. driver specifies the database driver name (e.g., "mysql", "postgres").
func NewSliceRowsScanner ¶
func NewSliceRowsScanner(rows [][]any) RowsScanner
NewSliceRowsScanner creates a RowsScanner from a 2D slice. Each inner slice represents a row of values.