Documentation
¶
Overview ¶
Package csv contains simple CSV parser
Index ¶
- Variables
- type Header
- 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) GetF32(index int) (float32, error)
- func (r Row) GetI(index int) (int, error)
- func (r Row) GetI8(index int) (int8, error)
- func (r Row) GetI16(index int) (int16, error)
- func (r Row) GetI32(index int) (int32, error)
- func (r Row) GetI64(index int) (int64, error)
- func (r Row) GetU(index int) (uint, error)
- func (r Row) GetU8(index int) (uint8, error)
- func (r Row) GetU16(index int) (uint16, error)
- func (r Row) GetU32(index int) (uint32, error)
- func (r Row) GetU64(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 is returned by the ReadTo method if empty destination slice was given ErrEmptyDest = errors.New("destination slice must not be empty") // ErrNilReader is returned when reader struct is nil ErrNilReader = errors.New("reader is nil") // ErrEmptyHeader is returned when header has no data ErrEmptyHeader = errors.New("header is empty") // ErrNilMap is returned when map is nil ErrNilMap = errors.New("map is nil") )
Functions ¶
This section is empty.
Types ¶
type Header ¶
type Header []string
Header is row with header data
func (Header) Map ¶
Map maps row data using headers names
Example ¶
fd, err := os.Open("file.csv")
if err != nil {
fmt.Println(err.Error())
return
}
defer fd.Close()
r := NewReader(fd, ';')
m := map[string]string{}
for {
row, err := r.Read()
if err == io.EOF {
break
}
r.Header.Map(m, row)
fmt.Printf("%#v\n", m)
}
type Reader ¶
type Reader struct {
Header Header
// contains filtered or unexported fields
}
Reader is CSV reader struct. It is NOT safe for concurrent use. Each Reader should be used by a single goroutine.
func NewReader ¶
NewReader creates a new CSV reader that reads from r, using comma as the field delimiter
func (*Reader) Line ¶
Line returns number of the last line read
Example ¶
fd, err := os.Open("file.csv")
if err != nil {
fmt.Println(err.Error())
return
}
defer fd.Close()
r := NewReader(fd, ';')
for {
row, err := r.Read()
if err == io.EOF {
break
}
if !strings.HasPrefix(row.Get(0), "id-") {
fmt.Printf("Invalid value in row %d: value in cell 0 must have \"id-\" prefix", r.Line())
return
}
}
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()
r := NewReader(fd, ';')
for {
row, err := r.Read()
if err == io.EOF {
break
}
fmt.Printf("%#v\n", row)
}
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()
r := NewReader(fd, ';')
row := make(Row, 10)
for {
err := r.ReadTo(row)
if err == io.EOF {
break
}
fmt.Printf("%#v\n", row)
}
func (*Reader) Seq ¶
Seq is an iterator over all CSV data using Go 1.23+ range-over-func. It reads each row sequentially until EOF or an error occurs. The yield function receives the current line number and row data. After Seq returns, call Error() to check if iteration stopped due to an error or reached EOF naturally.
Example ¶
fd, err := os.Open("file.csv")
if err != nil {
fmt.Println(err.Error())
return
}
defer fd.Close()
r := NewReader(fd, ';')
for line, row := range r.Seq {
fmt.Printf("%d: %#v\n", line, row)
}
func (*Reader) WithBufferSize ¶
WithBufferSize sets the maximum token size for the underlying scanner
func (*Reader) WithHeader ¶
WithHeader configures the reader to treat the first row as a header
Example ¶
fd, err := os.Open("file.csv")
if err != nil {
fmt.Println(err.Error())
return
}
defer fd.Close()
r := NewReader(fd, ';').WithHeader(true)
for {
row, err := r.Read()
if err == io.EOF {
break
}
fmt.Printf("%#v\n", row)
}
type Row ¶
type Row []string
Row is CSV row
func (Row) Cells ¶
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 ¶
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 ¶
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 ¶
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 ¶
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) GetF32 ¶
GetF32 returns cell value as float32
Example ¶
r := Row{"1", "John", "Doe", "0.34"}
id, err := r.GetI(0)
if err != nil {
panic(err.Error())
}
balance, err := r.GetF32(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 ¶
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) GetI8 ¶
GetI8 returns cell value as int8
Example ¶
r := Row{"1", "John", "Doe", "0.34"}
id, err := r.GetI8(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) GetI16 ¶
GetI16 returns cell value as int16
Example ¶
r := Row{"1", "John", "Doe", "0.34"}
id, err := r.GetI16(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) GetI32 ¶
GetI32 returns cell value as int32
Example ¶
r := Row{"1", "John", "Doe", "0.34"}
id, err := r.GetI32(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) GetI64 ¶
GetI64 returns cell value as int64
Example ¶
r := Row{"1", "John", "Doe", "0.34"}
id, err := r.GetI64(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 ¶
GetU returns cell value as uint
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) GetU8 ¶
GetU8 returns cell value as uint8
Example ¶
r := Row{"184", "user@domain.com", "Yes"}
id, err := r.GetU8(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: 184 Email: user@domain.com Is active: true
func (Row) GetU16 ¶
GetU16 returns cell value as uint16
Example ¶
r := Row{"18469", "user@domain.com", "Yes"}
id, err := r.GetU16(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: 18469 Email: user@domain.com Is active: true
func (Row) GetU32 ¶
GetU32 returns cell value as uint32
Example ¶
r := Row{"1846915341", "user@domain.com", "Yes"}
id, err := r.GetU32(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) GetU64 ¶
GetU64 returns cell value as uint64
Example ¶
r := Row{"1846915341", "user@domain.com", "Yes"}
id, err := r.GetU64(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 ¶
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 ¶
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 ¶
Size returns size of the row
Example ¶
r := Row{"1", "John", "Doe", "0.34"}
fmt.Printf("Size: %d\n", r.Size())
Output: Size: 4