Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( ErrHeaderCountDoesNotMatchRowCount = errors.New("header and row count must be identical") ErrRowCountIsSmallerThanTargetCount = errors.New("no more targets can be given than the available columns") )
Functions ¶
This section is empty.
Types ¶
type DictReader ¶
type DictReader struct {
// contains filtered or unexported fields
}
DictReader allows reading from csv.Reader for CSV files that have a header row. It will map each row to a map[string]string using the headings as the map keys.
func NewDictReader ¶
func NewDictReader(r *csv.Reader) *DictReader
func (*DictReader) Headers ¶ added in v0.2.0
func (r *DictReader) Headers() ([]string, error)
Headers returns the header row.
Example ¶
package main import ( "encoding/csv" "fmt" "strings" csvreader "github.com/Dan6erbond/csvparse" ) func main() { in := `first_name,last_name,username "Rob","Pike",rob Ken,Thompson,ken "Robert","Griesemer","gri" ` r := csv.NewReader(strings.NewReader(in)) dr := csvreader.NewDictReader(r) headers, _ := dr.Headers() fmt.Println(headers) }
Output: [first_name last_name username]
func (*DictReader) Read ¶
func (r *DictReader) Read() (map[string]string, error)
Read returns the next line from the *csv.Reader as a map. Just like csv.Reader.Read() it will return an io.EOF if no more lines are found.
Example ¶
package main import ( "encoding/csv" "fmt" "strings" csvreader "github.com/Dan6erbond/csvparse" ) func main() { in := `first_name,last_name,username "Rob","Pike",rob Ken,Thompson,ken "Robert","Griesemer","gri" ` r := csv.NewReader(strings.NewReader(in)) dr := csvreader.NewDictReader(r) row, _ := dr.Read() fmt.Println(row["first_name"]) }
Output: Rob
func (*DictReader) ReadAll ¶
func (r *DictReader) ReadAll() ([]map[string]string, error)
Read returns the next line from the *csv.Reader as a slice of maps.
Example ¶
package main import ( "encoding/csv" "fmt" "strings" csvreader "github.com/Dan6erbond/csvparse" ) func main() { in := `first_name,last_name,username "Rob","Pike",rob Ken,Thompson,ken "Robert","Griesemer","gri" ` r := csv.NewReader(strings.NewReader(in)) dr := csvreader.NewDictReader(r) records, _ := dr.ReadAll() fmt.Println(records[2]["first_name"]) }
Output: Robert
type ScanReader ¶ added in v0.3.0
type ScanReader struct {
// contains filtered or unexported fields
}
func NewScanReader ¶ added in v0.3.0
func NewScanReader(reader *csv.Reader, opts ...ScanReaderOption) *ScanReader
func (*ScanReader) Scan ¶ added in v0.3.0
func (sr *ScanReader) Scan(tgts ...*string) error
Example ¶
package main import ( "encoding/csv" "fmt" "strings" "github.com/Dan6erbond/csvparse" ) func main() { in := `first_name,last_name,username "Rob","Pike",rob Ken,Thompson,ken "Robert","Griesemer","gri" ` r := csv.NewReader(strings.NewReader(in)) sr := csvparse.NewScanReader(r, csvparse.WithHeaderRow) var ( fn string ln string un string ) sr.Scan(&fn, &ln, &un) fmt.Println(fn, ln, un) }
Output: Rob Pike rob
type ScanReaderOption ¶ added in v0.3.0
type ScanReaderOption = func(sr *ScanReader)
var ( WithHeaderRow ScanReaderOption = func(sr *ScanReader) { sr.headerRow = true } )
Click to show internal directories.
Click to hide internal directories.