csv

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2025 License: MIT Imports: 5 Imported by: 0

README

CSV

A fast and easy-to-use Go library for streaming CSV encoding/decoding with struct tags, inspired by encoding/json.

Installation

Use the package manager to install.

go get github.com/VincentBrodin/csv

Usage

Decoder
package main 

import (
	"fmt"
	"io"
	"os"
	"github.com/VincentBrodin/csv"
)

type MyStruct struct {
	Name  string `csv:"name"`
	Email string `csv:"email"`
	Age   int    `csv:"age"`
}

func main() {
	file, err := os.Open("myfile.csv") // RO
	if err != nil {
		panic(err)
	}
	defer file.Close()

	decoder := csv.NewDecoder(file)

	for {
		s := MyStruct{}
		if err := decoder.Decode(&s); err == io.EOF {
			break
		} else if err != nil {
			panic(err)
		}

		fmt.Println(s)
	}
}
Encoder
package main

import (
    "os"
	"github.com/VincentBrodin/csv"
)

type MyStruct struct {
	Name  string `csv:"name"`
	Email string `csv:"email"`
	Age   int    `csv:"age"`
}

func main() {
	file, err := os.Create("myfile.csv")
	if err != nil {
		panic(err)
	}
	defer file.Close()

	encoder := csv.NewEncoder(file)

	myStruct := MyStruct{
		Name:  "John Doe",
		Email: "john.doe@example.com",
		Age:   21,
	}
    if err := encoder.Encode(myStruct); err != nil {
        panic(err)
    }

    if err := encoder.Flush(); err != nil {
        panic(err)
    }
}

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Decoder

type Decoder struct {
	// contains filtered or unexported fields
}

func NewDecoder

func NewDecoder(r io.Reader) *Decoder

func (*Decoder) Decode

func (dec *Decoder) Decode(out any) error

type Encoder

type Encoder struct {
	// contains filtered or unexported fields
}

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

func (*Encoder) Encode

func (enc *Encoder) Encode(in any) error

func (*Encoder) Flush

func (enc *Encoder) Flush() error

Jump to

Keyboard shortcuts

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