Documentation ¶
Overview ¶
Package csv provides CsvReader and CsvWriter to process csv format file in the struct declaration style.
Package csv provides CsvReader and CsvWriter to process csv format file in the struct declaration style.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ColumnInfo ¶
type ColumnInfo struct { // Name in the csv header. HeaderName string // Field to lookup values in struct. LookupField string // Number of cells for this column. Applied when IsSlice = true NumSpan int // Max length of content in this column Limit int IsSlice bool IsJsonNull bool }
func GenerateColumnInfos ¶
func GenerateColumnInfos(typ reflect.Type) []*ColumnInfo
type CsvReader ¶
Example ¶
type TestStruct struct { Int int String string TagInt int `csv:"tag_i"` anonyInt int HiddenInt int `csv:"-"` NextString string `csv:"nextstring"` } input := `Int,String,tag_i,nextstring 1,2,3,6` t := &TestStruct{} buf := bytes.NewBufferString(input) r := NewCsvReader(buf) r.ReadStruct(t) fmt.Printf("%+v", t)
Output: &{Int:1 String:2 TagInt:3 anonyInt:0 HiddenInt:0 NextString:6}
func NewCsvReader ¶
func NewCsvReader(r io.Reader, opts ...ReaderOption) *CsvReader
func (*CsvReader) ReadAllStructs ¶
func (*CsvReader) ReadStruct ¶
type CsvWriter ¶
CsvWriter extends the encoding/csv writer, supporting writting struct, and shortcut to write to a file.
Example ¶
type TestStruct struct { Int int String string TagInt int `csv:"tag_i"` anonyInt int HiddenInt int `csv:"-"` NextString string `csv:"nextstring"` } t := TestStruct{ 1, "2", 3, 4, 5, "6", } buf := &bytes.Buffer{} w := NewCsvWriter(buf, WithAppendBOM(false)) w.WriteStruct(t) w.Flush() fmt.Println(buf.String())
Output: Int,String,tag_i,nextstring 1,2,3,6
func NewCsvWriter ¶
func NewCsvWriter(w io.Writer, opts ...WriterOption) *CsvWriter
func (*CsvWriter) WriteStruct ¶
type FileCsvReader ¶
func NewFileCsvReader ¶
func NewFileCsvReader(filename string) *FileCsvReader
func (*FileCsvReader) Close ¶
func (r *FileCsvReader) Close() error
type FileCsvWriter ¶
func NewFileCsvWriter ¶
func NewFileCsvWriter(filename string) *FileCsvWriter
NewFileCsvWriter is combination of creating file and passing it to a writer.
func (*FileCsvWriter) Close ¶
func (fcw *FileCsvWriter) Close() error
type ReaderOption ¶
type ReaderOption func(*CsvReader)
func WithReaderColumnInfos ¶
func WithReaderColumnInfos(infos []*ColumnInfo) ReaderOption
func WithReaderSkipJSONNull ¶
func WithReaderSkipJSONNull(skip bool) ReaderOption
func WithReaderSliceDelimiter ¶
func WithReaderSliceDelimiter(d string) ReaderOption
type WriterOption ¶
type WriterOption func(*CsvWriter)
func WithAppendBOM ¶
func WithAppendBOM(enabled bool) WriterOption
func WithColumnInfos ¶
func WithColumnInfos(infos []*ColumnInfo) WriterOption
func WithSkipJSONNull ¶
func WithSkipJSONNull(skip bool) WriterOption
func WithSliceDelimiter ¶
func WithSliceDelimiter(d string) WriterOption
Click to show internal directories.
Click to hide internal directories.