Documentation ¶
Overview ¶
Package csv implements a parser and generator for CSV-like documents.
Index ¶
- func Marshal(v interface{}, settings ...Setting) ([]byte, error)
- func Unmarshal(data []byte, dest interface{}, settings ...Setting) error
- type Generator
- type InvalidUnmarshalError
- type Scanner
- type Setting
- func AllowEmptyField(v bool) Setting
- func AllowEndingLineBreakInLastRecord(v bool) Setting
- func AllowSingleQuote(v bool) Setting
- func Comment(comment rune) Setting
- func Encoding(enc encoding.Encoding) Setting
- func FieldPrefix(prefix rune) Setting
- func FieldSuffix(suffix rune) Setting
- func HeaderPrefix(prefix rune) Setting
- func HeaderSuffix(suffix rune) Setting
- func IgnoreBOM(v bool) Setting
- func OmitEmptyLine(v bool) Setting
- func OmitLeadingSpace(v bool) Setting
- func OmitTrailingSpace(v bool) Setting
- func Prefix(prefix rune) Setting
- func RFC4180() Setting
- func Separator(sep rune) Setting
- func Suffix(suffix rune) Setting
- func Validator(name string, validator func(interface{}) bool) Setting
- func WriteHeader(v bool) Setting
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Marshal ¶
Marshal generates a CSV document from v with the given settings.
v should be an array/slice of struct or struct pointers. In these structs, each exported field will be marshaled as a CSV field, with the field name as the column header. This can be customized with a "csv" struct field tag, which gives the name of the field. Use "-" to omit a field from being marshaled. Use "-," to set the header name to "-".
Below are some example of using the "csv" struct field tag.
// Field will be marshaled with "myName" as its header name. Field int `csv:"myName"` // Field is ignored. Field int `csv:"-"` // Field will be marshaled with "-" as its header name. Field int `csv:"-,"`
Marshal supports the following types:
A boolean value will be marshaled to "true" or "false" based on its value.
A floating point, integer or number value will be marshaled to the string representation of its value.
A string value will be marshaled to the value of itself.
Any type implementing encoding.TextMarshaler will be marshaled to the value returned by MarshalText.
If Marshal encounters a field with an unsupported type, an UnsupportedTypeError will be returned.
In order to marshal an unsupported type, a translator can be used to translate the value. A translator is a func(interface{}) ([]byte, error). Use the Translator setting to register one or more translators before marshaling. For example:
func TranslateIntSlice(slice interface{}) ([]byte, error) { ... } csv.Marshal(..., csv.Translator("intSlice", TranslateIntSlice))
To use a translator for an unsupported type, add it to the "csv" struct field tag. For example:
// Field will be marshaled with "myName" as its header name, and use // translator with name "intSlice" to translate its value. Field []int `csv:"myName,intSlice"`
If a field has multiple ways to be marshaled, the order of using these ways is:
- Using the translator specified in "csv" struct field tag.
- Call MarshalText of the field.
- Use the default way to marshal the field if it is supported.
Types ¶
type Generator ¶
type Generator struct {
// contains filtered or unexported fields
}
A Generator generates a new CSV document.
func NewGenerator ¶
NewGenerator creates and returns a new generator with the given settings.
func (*Generator) Finish ¶
Finish finishes writing to the generator and returns data of the document.
After calling Finish, the generator can no longer be written. Any call to Write and WriteAll will return an error.
type InvalidUnmarshalError ¶
An InvalidUnmarshalError describes an invalid argument passed to Unmarshal. (The argument to Unmarshal must be a non-nil pointer.)
func (*InvalidUnmarshalError) Error ¶
func (e *InvalidUnmarshalError) Error() string
type Scanner ¶
type Scanner struct {
// contains filtered or unexported fields
}
A Scanner scans a CSV document and returns the scanned header and rows.
func NewScanner ¶
NewScanner creates and returns a new scanner from a byte slice with the given settings.
func (*Scanner) Scan ¶
Scan scans the next row from the CSV document.
If an error occurs, row will be returned as nil.
If there is no more row to be scanned, io.EOF will be returned.
type Setting ¶
type Setting func(*rule)
A Setting provides information on how documents should be parsed.
func AllowEmptyField ¶
AllowEmptyField sets whether empty fields are allowed while reading a document.
func AllowEndingLineBreakInLastRecord ¶
AllowEndingLineBreakInLastRecord sets whether the last record may have an ending line break while reading a document.
func AllowSingleQuote ¶
AllowSingleQuote sets whether single quotes are allowed while reading a document.
func FieldPrefix ¶
FieldPrefix sets the prefix rune of fields while unmarshaling and marshaling a document.
If a field prefix is set, the Prefix setting will be ignored while reading and writing fields, but will still be used for the header.
func FieldSuffix ¶
FieldSuffix sets the suffix rune of fields while unmarshaling and marshaling a document.
If a field suffix is set, the Suffix setting will be ignored while reading and writing fields, but will still be used for the header.
func HeaderPrefix ¶
HeaderPrefix sets the prefix rune of header names while unmarshaling and marshaling a document.
If a header prefix is set, the Prefix setting will be ignored while reading and writing the header row, but will still be used for fields.
func HeaderSuffix ¶
HeaderSuffix sets the suffix rune of header names while unmarshaling and marshaling a document.
If a header suffix is set, the Suffix setting will be ignored while reading and writing the header row, but will still be used for fields.
func IgnoreBOM ¶
IgnoreBOM sets whether the leading BOM (byte order mark) should be ignored while reading a document. If not, the BOM will be treated as normal content.
This should not be done by a csv package, but since Golang has no built-in support for BOM, a workaround is required.
func OmitEmptyLine ¶
OmitEmptyLine sets whether empty lines should be omitted while reading a document.
func OmitLeadingSpace ¶
OmitLeadingSpace sets whether the leading spaces of fields should be omitted while reading a document.
func OmitTrailingSpace ¶
OmitTrailingSpace sets whether the trailing spaces of fields should be omitted while reading a document.
func RFC4180 ¶
func RFC4180() Setting
RFC4180 sets the parser and generator to work in the exact way as described in RFC 4180.
func Separator ¶
Separator sets the separator used to separate fields while reading and writing a document.
func Validator ¶
Validator adds a new validator functions for validating a CSV value while unmarshaling a document.
func WriteHeader ¶
WriteHeader sets whether to output the header row while writing the document.