"std:encoding/csv"Provides reading and writing of comma-separated values (CSV) files as defined by RFC 4180. Supports configurable delimiters, quoted fields with embedded commas/newlines, and escaped double-quotes.
View source on Codeberg →type Error int
const (
OK Error = iota
EOF
ErrIO
ErrQuote
ErrFieldCount
)
Error represents the result of a CSV operation. OK indicates success; EOF signals end of input; ErrIO indicates an underlying I/O error; ErrQuote means a malformed quoted field was encountered; ErrFieldCount means a row had an unexpected number of fields.
type Reader struct { /* unexported fields */ }
Reader reads records from a CSV-encoded input stream.
type Writer struct { /* unexported fields */ }
Writer writes records in CSV format to an output stream.
func NewReader(r io.Reader) Reader
Creates a new Reader that reads CSV records from the given io.Reader.
func SetDelimiter(r *Reader, d byte)
Sets the field delimiter for the reader. The default delimiter is a comma (',').
func LineNum(r *Reader) int
Returns the current line number in the input stream. Useful for error reporting.
func Read(r *Reader) (list[string], Error)
Reads one record (a slice of fields) from the reader. Returns the record and an Error. On success the error is OK; at end of input it is EOF.
func ReadAll(r *Reader) (list[list[string]], Error)
Reads all remaining records from the reader. Returns a slice of records and an Error indicating whether reading completed successfully.
func FreeReader(r *Reader)
Releases all resources associated with the given Reader.
func FreeRecords(records list[list[string]])
Frees all memory associated with a slice of records returned by ReadAll.
func NewWriter(w io.Writer) Writer
Creates a new Writer that writes CSV records to the given io.Writer.
func SetWriterDelimiter(w *Writer, d byte)
Sets the field delimiter for the writer. The default delimiter is a comma (',').
func WriteRow(w *Writer, fields []string) Error
Writes a single CSV record (row) to the writer. Fields containing the delimiter, double-quotes, or newlines are automatically quoted. Returns an Error indicating success or failure.
func WriteAll(w *Writer, rows [][]string) Error
Writes multiple CSV records to the writer and flushes. Returns an Error indicating whether all rows were written successfully.
func Flush(w *Writer) Error
Flushes the writer. This is a no-op for compatibility since the writer writes through immediately on each WriteRow call. Always returns OK.
func FreeWriter(w *Writer)
Releases all resources associated with the given Writer.