csv

package
v12.110.1 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2024 License: Apache-2.0 Imports: 4 Imported by: 1

Documentation

Overview

Package csv contains simple CSV parser

Index

Examples

Constants

This section is empty.

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
	// 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) Read

func (r *Reader) Read() ([]string, 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()

reader := NewReader(fd)
reader.Comma = ','

for {
	data, err := reader.Read()

	if err == io.EOF {
		break
	}

	fmt.Printf("%#v\n", data)
}
Output:

func (*Reader) ReadTo

func (r *Reader) ReadTo(dst []string) 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()

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

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()

reader := NewReader(fd).WithComma(',')

for {
	data, err := reader.Read()

	if err == io.EOF {
		break
	}

	fmt.Printf("%#v\n", data)
}
Output:

Jump to

Keyboard shortcuts

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