Documentation ¶
Overview ¶
Package csv contains simple CSV parser
Index ¶
- Variables
- type Reader
- type Row
- func (r Row) Cells() int
- func (r Row) ForEach(fn func(index int, value string) error) error
- func (r Row) Get(index int) string
- func (r Row) GetB(index int) bool
- func (r Row) GetF(index int) (float64, error)
- func (r Row) GetI(index int) (int, error)
- func (r Row) GetU(index int) (uint64, error)
- func (r Row) Has(index int) bool
- func (r Row) IsEmpty() bool
- func (r Row) Size() int
- func (r Row) ToBytes(comma rune) []byte
- func (r Row) ToString(comma rune) string
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrEmptyDest = errors.New("Destination slice length must be greater than 1")
ErrEmptyDest is returned by the ReadTo method if empty destination slice was given
var ErrNilReader = errors.New("Reader is nil")
ErrNilReader is returned when reader struct is nil
Functions ¶
This section is empty.
Types ¶
type Reader ¶
Reader is CSV reader struct
func (*Reader) Read ¶
Read reads line from CSV file
Example ¶
fd, err := os.Open("file.csv") if err != nil { fmt.Println(err.Error()) return } defer fd.Close() reader := NewReader(fd) reader.Comma = ',' for { data, err := reader.Read() if err == io.EOF { break } fmt.Printf("%#v\n", data) }
Output:
func (*Reader) ReadTo ¶
ReadTo reads data to given slice
Example ¶
fd, err := os.Open("file.csv") if err != nil { fmt.Println(err.Error()) return } defer fd.Close() reader := NewReader(fd) reader.Comma = ',' data := make([]string, 10) for { err := reader.ReadTo(data) if err == io.EOF { break } fmt.Printf("%#v\n", data) }
Output:
func (*Reader) WithComma ¶ added in v12.69.0
WithComma sets comma (fields delimiter) for CSV reader
Example ¶
fd, err := os.Open("file.csv") if err != nil { fmt.Println(err.Error()) return } defer fd.Close() reader := NewReader(fd).WithComma(',') for { data, err := reader.Read() if err == io.EOF { break } fmt.Printf("%#v\n", data) }
Output:
func (*Reader) WithHeaderSkip ¶ added in v12.122.0
WithHeaderSkip sets header skip flag
Example ¶
fd, err := os.Open("file.csv") if err != nil { fmt.Println(err.Error()) return } defer fd.Close() reader := NewReader(fd).WithHeaderSkip(true) for { data, err := reader.Read() if err == io.EOF { break } fmt.Printf("%#v\n", data) }
Output:
type Row ¶ added in v12.122.0
type Row []string
Row is CSV row
func (Row) Cells ¶ added in v12.122.0
Cells returns number of cells filled with data
Example ¶
r := Row{"1", "", "", "0.34"} fmt.Printf("Size: %d\n", r.Size()) fmt.Printf("Cells: %d\n", r.Cells())
Output: Size: 4 Cells: 2
func (Row) ForEach ¶ added in v12.122.0
ForEach executes given function for every cell in a row
Example ¶
r := Row{"John", "Do"} err := r.ForEach(func(index int, value string) error { if len(value) < 3 { return fmt.Errorf("Cell %d contains invalid value %q", index, value) } return nil }) fmt.Println(err)
Output: Cell 1 contains invalid value "Do"
func (Row) Get ¶ added in v12.122.0
Get returns value of the cell with given index
Example ¶
r := Row{"1", "John", "Doe", "0.34"} id, err := r.GetI(0) if err != nil { panic(err.Error()) } balance, err := r.GetF(3) if err != nil { panic(err.Error()) } fmt.Printf("ID: %d\n", id) fmt.Printf("First name: %s\n", r.Get(1)) fmt.Printf("Last name: %s\n", r.Get(2)) fmt.Printf("Balance: %g\n", balance)
Output: ID: 1 First name: John Last name: Doe Balance: 0.34
func (Row) GetB ¶ added in v12.122.0
GetB returns cell value as boolean
Example ¶
r := Row{"1846915341", "user@domain.com", "Yes"} id, err := r.GetU(0) if err != nil { panic(err.Error()) } fmt.Printf("ID: %d\n", id) fmt.Printf("Email: %s\n", r.Get(1)) fmt.Printf("Is active: %t\n", r.GetB(2))
Output: ID: 1846915341 Email: user@domain.com Is active: true
func (Row) GetF ¶ added in v12.122.0
GetF returns cell value as float
Example ¶
r := Row{"1", "John", "Doe", "0.34"} id, err := r.GetI(0) if err != nil { panic(err.Error()) } balance, err := r.GetF(3) if err != nil { panic(err.Error()) } fmt.Printf("ID: %d\n", id) fmt.Printf("First name: %s\n", r.Get(1)) fmt.Printf("Last name: %s\n", r.Get(2)) fmt.Printf("Balance: %g\n", balance)
Output: ID: 1 First name: John Last name: Doe Balance: 0.34
func (Row) GetI ¶ added in v12.122.0
GetI returns cell value as int
Example ¶
r := Row{"1", "John", "Doe", "0.34"} id, err := r.GetI(0) if err != nil { panic(err.Error()) } balance, err := r.GetF(3) if err != nil { panic(err.Error()) } fmt.Printf("ID: %d\n", id) fmt.Printf("First name: %s\n", r.Get(1)) fmt.Printf("Last name: %s\n", r.Get(2)) fmt.Printf("Balance: %g\n", balance)
Output: ID: 1 First name: John Last name: Doe Balance: 0.34
func (Row) GetU ¶ added in v12.122.0
GetU returns cell value as uint64
Example ¶
r := Row{"1846915341", "user@domain.com", "Yes"} id, err := r.GetU(0) if err != nil { panic(err.Error()) } fmt.Printf("ID: %d\n", id) fmt.Printf("Email: %s\n", r.Get(1)) fmt.Printf("Is active: %t\n", r.GetB(2))
Output: ID: 1846915341 Email: user@domain.com Is active: true
func (Row) Has ¶ added in v12.122.0
Has returns true if row contains cell with given index filled with data
Example ¶
r := Row{"1", "John", "", "0.34"} fmt.Printf("Has cell 1: %t\n", r.Has(1)) fmt.Printf("Has cell 2: %t\n", r.Has(2)) fmt.Printf("Has cell 100: %t\n", r.Has(100))
Output: Has cell 1: true Has cell 2: false Has cell 100: false
func (Row) IsEmpty ¶ added in v12.122.0
IsEmpty returns true if all cells are empty
Example ¶
r1 := Row{"1", "John", "Doe", "0.34"} r2 := Row{"", "", "", ""} fmt.Printf("r1 is empty: %t\n", r1.IsEmpty()) fmt.Printf("r2 is empty: %t\n", r2.IsEmpty())
Output: r1 is empty: false r2 is empty: true
func (Row) Size ¶ added in v12.122.0
Size returns size of the row
Example ¶
r := Row{"1", "John", "Doe", "0.34"} fmt.Printf("Size: %d\n", r.Size())
Output: Size: 4