csv

package
v12.124.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 12, 2024 License: Apache-2.0 Imports: 6 Imported by: 1

Documentation

Overview

Package csv contains simple CSV parser

Index

Examples

Constants

View Source
const COMMA = ';'

COMMA is default separator for CSV cells

Variables

View Source
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

View Source
var ErrNilReader = errors.New("Reader is nil")

ErrNilReader is returned when reader struct is nil

Functions

This section is empty.

Types

type Reader

type Reader struct {
	Comma      rune
	SkipHeader bool
	// contains filtered or unexported fields
}

Reader is CSV reader struct

func NewReader

func NewReader(r io.Reader) *Reader

NewReader create new CSV reader

func (*Reader) Line added in v12.123.0

func (r *Reader) Line() int

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

func (r *Reader) Read() (Row, error)

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

func (r *Reader) ReadTo(dst Row) error

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 added in v12.69.0

func (r *Reader) WithComma(comma rune) *Reader

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 added in v12.122.0

func (r *Reader) WithHeaderSkip(flag bool) *Reader

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 added in v12.122.0

type Row []string

Row is CSV row

func (Row) Cells added in v12.122.0

func (r Row) Cells() int

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

func (r Row) ForEach(fn func(index int, value string) error) error

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

func (r Row) Get(index int) string

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

func (r Row) GetB(index int) bool

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

func (r Row) GetF(index int) (float64, error)

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 added in v12.123.0

func (r Row) GetF32(index int) (float32, error)

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 added in v12.122.0

func (r Row) GetI(index int) (int, error)

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 added in v12.123.0

func (r Row) GetI16(index int) (int16, error)

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 added in v12.123.0

func (r Row) GetI32(index int) (int32, error)

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 added in v12.123.0

func (r Row) GetI64(index int) (int64, error)

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 added in v12.123.0

func (r Row) GetI8(index int) (int8, error)

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 added in v12.122.0

func (r Row) GetU(index int) (uint, error)

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 added in v12.123.0

func (r Row) GetU16(index int) (uint16, error)

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 added in v12.123.0

func (r Row) GetU32(index int) (uint32, error)

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 added in v12.123.0

func (r Row) GetU64(index int) (uint64, error)

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 added in v12.123.0

func (r Row) GetU8(index int) (uint8, error)

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 added in v12.122.0

func (r Row) Has(index int) bool

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

func (r Row) IsEmpty() bool

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

func (r Row) Size() int

Size returns size of the row

Example
r := Row{"1", "John", "Doe", "0.34"}

fmt.Printf("Size: %d\n", r.Size())
Output:

Size: 4

func (Row) ToBytes added in v12.122.0

func (r Row) ToBytes(comma rune) []byte

ToBytes returns representation of row as a byte slice

Example
r := Row{"1", "John", "Doe", "0.34"}

fmt.Println(string(r.ToBytes(';')))
Output:

1;John;Doe;0.34

func (Row) ToString added in v12.122.0

func (r Row) ToString(comma rune) string

ToString returns string representation of row

Example
r := Row{"1", "John", "Doe", "0.34"}

fmt.Println(r.ToString(';'))
Output:

1;John;Doe;0.34

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL