Documentation ¶
Overview ¶
Package csv contains simple CSV parser
Index ¶
- Constants
- 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) GetF32(index int) (float32, error)
- func (r Row) GetI(index int) (int, 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) GetI8(index int) (int8, error)
- func (r Row) GetU(index int) (uint, 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) GetU8(index int) (uint8, 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 ¶
const COMMA = ';'
COMMA is default separator for CSV cells
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) 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 } }
Output:
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) }
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() r := NewReader(fd) row := make(Row, 10) for { err := r.ReadTo(row) if err == io.EOF { break } fmt.Printf("%#v\n", row) }
Output:
func (*Reader) WithComma ¶
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() r := NewReader(fd).WithComma(',') for { row, err := r.Read() if err == io.EOF { break } fmt.Printf("%#v\n", row) }
Output:
func (*Reader) WithHeaderSkip ¶
WithHeaderSkip sets header skip flag
Example ¶
fd, err := os.Open("file.csv") if err != nil { fmt.Println(err.Error()) return } defer fd.Close() r := NewReader(fd).WithHeaderSkip(true) for { row, err := r.Read() if err == io.EOF { break } fmt.Printf("%#v\n", row) }
Output:
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) 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) 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) 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) 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) 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) 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