Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type FieldReader ¶
type FieldReader struct {
// Comma is the field delimiter.
// It is set to comma (',') by NewReader.
Comma rune
// Comment, if not 0, is the comment character. Lines beginning with the
// Comment character without preceding whitespace are ignored.
// With leading whitespace the Comment character becomes part of the
// field, even if TrimLeadingSpace is true.
Comment rune
// If LazyQuotes is true, a quote may appear in an unquoted field and a
// non-doubled quote may appear in a quoted field.
LazyQuotes bool
// If TrimLeadingSpace is true, leading white space in a field is ignored.
// This is done even if the field delimiter, Comma, is white space.
TrimLeadingSpace bool
// FieldNames are the names for the fields on each row. If FieldNames is
// left nil, it will be set to the first row read.
FieldNames []string
// contains filtered or unexported fields
}
FieldReader is a wrapper around encoding/csv.Reader that allows reference to columns in a CSV source by field names. Its usage is like bufio.Scanner.
Example ¶
package main
import (
"fmt"
"log"
"strings"
"github.com/carlmjohnson/csv"
)
func main() {
in := `first_name,last_name,username
"Rob","Pike",rob
Ken,Thompson,ken
"Robert","Griesemer","gri"
`
r := csv.NewFieldReader(strings.NewReader(in))
for r.Scan() {
fmt.Println(r.Field("username"))
}
if err := r.Err(); err != nil {
log.Fatal(err)
}
}
Output: rob ken gri
Example (Options) ¶
This example shows how csv.FieldReader can be configured to handle other types of CSV files.
package main
import (
"fmt"
"log"
"strings"
"github.com/carlmjohnson/csv"
)
func main() {
in := `first_name;last_name;username
"Rob";"Pike";rob
# lines beginning with a # character are ignored
Ken;Thompson;ken
"Robert";"Griesemer";"gri"
`
r := csv.NewFieldReader(strings.NewReader(in))
r.Comma = ';'
r.Comment = '#'
for r.Scan() {
fmt.Println(r.Field("username"))
}
if err := r.Err(); err != nil {
log.Fatal(err)
}
}
Output: rob ken gri
func NewFieldReader ¶
func NewFieldReader(r io.Reader) *FieldReader
NewFieldReader returns a new FieldReader that reads from r.
func (*FieldReader) Err ¶
func (f *FieldReader) Err() error
Err returns any errors encountered by the FieldReader, except io.EOF, which is not considered an error in normal operation.
func (*FieldReader) Field ¶
func (f *FieldReader) Field(fieldname string) string
Field returns the value in the currently loaded row of the column corresponding to fieldname.
func (*FieldReader) Fields ¶
func (f *FieldReader) Fields() map[string]string
Fields returns a map from fieldnames to values for the current row.
func (*FieldReader) ReadAll ¶
func (f *FieldReader) ReadAll() ([]map[string]string, error)
ReadAll consumes the underlying io.Reader and returns a slice of maps for each row.
func (*FieldReader) Scan ¶
func (f *FieldReader) Scan() bool
Scan loads the next row into the FieldReader. It returns false upon encountering an error in the underlying io.Reader.