Imports #
"bufio"
"bytes"
"errors"
"fmt"
"io"
"unicode"
"unicode/utf8"
"bufio"
"io"
"strings"
"unicode"
"unicode/utf8"
"bufio"
"bytes"
"errors"
"fmt"
"io"
"unicode"
"unicode/utf8"
"bufio"
"io"
"strings"
"unicode"
"unicode/utf8"
These are the errors that can be returned in [ParseError.Err].
var ErrBareQuote = *ast.CallExpr
These are the errors that can be returned in [ParseError.Err].
var ErrFieldCount = *ast.CallExpr
These are the errors that can be returned in [ParseError.Err].
var ErrQuote = *ast.CallExpr
Deprecated: ErrTrailingComma is no longer used.
var ErrTrailingComma = *ast.CallExpr
var errInvalidDelim = *ast.CallExpr
A ParseError is returned for parsing errors. Line and column numbers are 1-indexed.
type ParseError struct {
StartLine int
Line int
Column int
Err error
}
A Reader reads records from a CSV-encoded file. As returned by [NewReader], a Reader expects input conforming to RFC 4180. The exported fields can be changed to customize the details before the first call to [Reader.Read] or [Reader.ReadAll]. The Reader converts all \r\n sequences in its input to plain \n, including in multiline field values, so that the returned data does not depend on which line-ending convention an input file uses.
type Reader struct {
Comma rune
Comment rune
FieldsPerRecord int
LazyQuotes bool
TrimLeadingSpace bool
ReuseRecord bool
TrailingComma bool
r *bufio.Reader
numLine int
offset int64
rawBuffer []byte
recordBuffer []byte
fieldIndexes []int
fieldPositions []position
lastRecord []string
}
A Writer writes records using CSV encoding. As returned by [NewWriter], a Writer writes records terminated by a newline and uses ',' as the field delimiter. The exported fields can be changed to customize the details before the first call to [Writer.Write] or [Writer.WriteAll]. [Writer.Comma] is the field delimiter. If [Writer.UseCRLF] is true, the Writer ends each output line with \r\n instead of \n. The writes of individual records are buffered. After all data has been written, the client should call the [Writer.Flush] method to guarantee all data has been forwarded to the underlying [io.Writer]. Any errors that occurred should be checked by calling the [Writer.Error] method.
type Writer struct {
Comma rune
UseCRLF bool
w *bufio.Writer
}
pos holds the position of a field in the current line.
type position struct {
line int
col int
}
Error reports any error that has occurred during a previous [Writer.Write] or [Writer.Flush].
func (w *Writer) Error() error
func (e *ParseError) Error() string
FieldPos returns the line and column corresponding to the start of the field with the given index in the slice most recently returned by [Reader.Read]. Numbering of lines and columns starts at 1; columns are counted in bytes, not runes. If this is called with an out-of-bounds index, it panics.
func (r *Reader) FieldPos(field int) (line int, column int)
Flush writes any buffered data to the underlying [io.Writer]. To check if an error occurred during Flush, call [Writer.Error].
func (w *Writer) Flush()
InputOffset returns the input stream byte offset of the current reader position. The offset gives the location of the end of the most recently read row and the beginning of the next row.
func (r *Reader) InputOffset() int64
NewReader returns a new Reader that reads from r.
func NewReader(r io.Reader) *Reader
NewWriter returns a new Writer that writes to w.
func NewWriter(w io.Writer) *Writer
Read reads one record (a slice of fields) from r. If the record has an unexpected number of fields, Read returns the record along with the error [ErrFieldCount]. If the record contains a field that cannot be parsed, Read returns a partial record along with the parse error. The partial record contains all fields read before the error. If there is no data left to be read, Read returns nil, [io.EOF]. If [Reader.ReuseRecord] is true, the returned slice may be shared between multiple calls to Read.
func (r *Reader) Read() (record []string, err error)
ReadAll reads all the remaining records from r. Each record is a slice of fields. A successful call returns err == nil, not err == [io.EOF]. Because ReadAll is defined to read until EOF, it does not treat end of file as an error to be reported.
func (r *Reader) ReadAll() (records [][]string, err error)
func (e *ParseError) Unwrap() error
Write writes a single CSV record to w along with any necessary quoting. A record is a slice of strings with each string being one field. Writes are buffered, so [Writer.Flush] must eventually be called to ensure that the record is written to the underlying [io.Writer].
func (w *Writer) Write(record []string) error
WriteAll writes multiple CSV records to w using [Writer.Write] and then calls [Writer.Flush], returning any error from the Flush.
func (w *Writer) WriteAll(records [][]string) error
fieldNeedsQuotes reports whether our field must be enclosed in quotes. Fields with a Comma, fields with a quote or newline, and fields which start with a space must be enclosed in quotes. We used to quote empty strings, but we do not anymore (as of Go 1.4). The two representations should be equivalent, but Postgres distinguishes quoted vs non-quoted empty string during database imports, and it has an option to force the quoted behavior for non-quoted CSV but it has no option to force the non-quoted behavior for quoted CSV, making CSV with quoted empty strings strictly less useful. Not quoting the empty string also makes this package match the behavior of Microsoft Excel and Google Drive. For Postgres, quote the data terminating string `\.`.
func (w *Writer) fieldNeedsQuotes(field string) bool
lengthNL reports the number of bytes for the trailing \n.
func lengthNL(b []byte) int
nextRune returns the next rune in b or utf8.RuneError.
func nextRune(b []byte) rune
readLine reads the next line (with the trailing endline). If EOF is hit without a trailing endline, it will be omitted. If some bytes were read, then the error is never [io.EOF]. The result is only valid until the next call to readLine.
func (r *Reader) readLine() ([]byte, error)
func (r *Reader) readRecord(dst []string) ([]string, error)
func validDelim(r rune) bool
Generated with Arrow