Giter Site home page Giter Site logo

vcsv's Introduction

VCSV - Vertigo CSV Reader for Go

vcsv is a Go package providing a flexible and powerful way to read and unmarshal CSV data into Go structs. It supports custom CSV headers, various data types, and user-defined parsing rules through struct tags.

Features

  • Read CSV data and unmarshal into Go structs.
  • Support for custom CSV headers.
  • Handle various primitive types and custom types implementing encoding.TextUnmarshaler.
  • Options such as format to specify the date format for time.Time fields.
  • Flexible configuration options for CSV parsing.
  • No external dependencies. Only uses the standard library.

Installation

To install VCSV, use the following command:

go get -u github.com/fond-of-vertigo/vcsv

Usage

Below are some examples of how to use the VCSV package.

Basic Usage

First, define a struct that maps to your CSV format:

import "time"

type Person struct {
    Name          string    `csv:"name"`
    Age           int       `csv:"age"`
    Birthdate     time.Time `csv:"birthdate,format:2006-01-02"`
    IsBirthday    bool      `csv:"is_birthday"`
}

When a CSV file contains no header, you can use the index tag to specify the column index instead of the column name:

type Person struct {
    Name          string    `csv:"index:0"`
    Age           int       `csv:"index:1"`
    Birthdate     time.Time `csv:"index:2,format:2006-01-02"`
    IsBirthday    bool      `csv:"index:3"`
}

Then, use VCSV to read and unmarshal data:

package main

import (
	"fmt"
	"github.com/fond-of-vertigo/vcsv"
	"os"
)

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

	reader, err := vcsv.New(file, vcsv.WithSeparationChar(','))
	if err != nil {
		panic(err)
	}

	var p Person
	for reader.Next(&err) {
        // Unmarshal the current line into the Person struct.
		if err := reader.UnmarshalLine(&p); err != nil { 
			panic(err)
		}
        
		fmt.Printf("%+v\n", p)
	}
}

Configuration Options

VCSV provides several options to configure the CSV reader:

  • WithHeader([]string): Sets the CSV header columns manually.
  • WithSeparationChar(rune): Sets a custom column separation character.
  • WithReadHeader(int): Specifies which line of the CSV file contains the header.

Example:

reader, err := vcsv.New(file, vcsv.WithSeparationChar(';'), vcsv.WithHeader([]string{"Name", "Age", "Birthdate", "IsBirthday"}))

vcsv's People

Contributors

coffeeri avatar

Watchers

Daniel Rose avatar Andre Dankbar avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.