csv

package
v14.1.7 Latest Latest
Warning

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

Go to latest
Published: May 13, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package csv contains simple CSV parser

Index

Examples

Constants

This section is empty.

Variables

View Source
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 []string

Header is row with header data

func (Header) Map

func (h Header) Map(m map[string]string, r Row) error

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

func (Header) ToLower

func (h Header) ToLower()

ToLower converts all headers to lower case

func (Header) ToUpper

func (h Header) ToUpper()

ToUpper converts all headers to upper case

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

func NewReader(r io.Reader, comma rune) *Reader

NewReader creates a new CSV reader that reads from r, using comma as the field delimiter

func (*Reader) Error

func (r *Reader) Error() error

Error returns error from underlying scanner

func (*Reader) Line

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
	}
}

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

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

func (*Reader) Seq

func (r *Reader) Seq(yield func(line int, row Row) bool)

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

func (r *Reader) WithBufferSize(n int) *Reader

WithBufferSize sets the maximum token size for the underlying scanner

func (*Reader) WithHeader

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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