csv

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2025 License: MIT Imports: 10 Imported by: 0

README

csv

test Go Report Card codecov Version Badge License Badge Go Reference

The Go library for serializing/deserializing CSV to/from Go structs.

Features

  • Map CSV headers to struct fields using csv tags (or field name when tag omitted).
  • Support basic types: string, ints, uints, floats, bool.
  • Easy to use API for marshaling and unmarshaling.

Installation

Run the following command to install the package:

go get github.com/ghosind/go-csv

Getting Started

Here's a quick example to get you started:

  1. Define a struct with csv tags:
type Person struct {
  Name string `csv:"name"`
  Age  int    `csv:"age"`
}
  1. Prepare CSV data:
name,age
Alice,30
Bob,25
  1. Unmarshal CSV into a slice:
var people []Person
err := csv.Unmarshal([]byte(data), &people)
log.Print(people)
// Output:
// [{Alice 30} {Bob 25}]
  1. Marshal a slice back to CSV:
data, err := csv.Marshal(people)
log.Print(string(data))
// Output:
// name,age
// Alice,30
// Bob,25

Tests

Run tests using the following command:

go test ./...

License

This project is licensed under the MIT License, see the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidType      = errors.New("csv: invalid type")
	ErrUnsupportedType  = errors.New("csv: unsupported type")
	ErrCannotSet        = errors.New("csv: cannot set value to nil pointer")
	ErrInvalidUnmarshal = errors.New("csv: Unmarshal(nil)")
)

Functions

func Marshal

func Marshal(v any) ([]byte, error)

func Unmarshal

func Unmarshal(data []byte, v any) error

Types

type Marshaler

type Marshaler interface {
	MarshalCSV() ([]byte, error)
}

type Unmarshaler

type Unmarshaler interface {
	UnmarshalCSV([]byte) error
}

Jump to

Keyboard shortcuts

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