csvparser

package module
v0.0.0-...-3be2f4e Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2025 License: Apache-2.0 Imports: 5 Imported by: 0

README

csvparser

The csvparser is a library to parse cvs file. It can marshal slice of struct to csv and vice-versa. It also has encoder and decoder.

Examples

Marshal
Write in standard output(terminal/consule)
package main

import (
	"fmt"
	"os"

	"github.com/Prithvipal/csvparser"
)

type Book struct {
	ID     int     `csv:"id"`
	Title  string  `csv:"book_title"`
	Price  float32 `csv:"price"`
	Author string  `csv:"name_of_author"`
}

func main() {
	b := []Book{
		{
			ID:     101,
			Title:  "Let us C",
			Price:  1232,
			Author: "Auth one",
		},
		{
			ID:     102,
			Title:  "Hands on go programming",
			Price:  334,
			Author: "Prithvipal Singh",
		},
	}
	enc := csvparser.NewEncoder(os.Stdout)
	err := enc.Encode(b)
	if err != nil {
		fmt.Println(err)
	}

}

Output

id,book_title,price,name_of_author
101,Let us C,1.232E+03,Auth one
102,Hands on go programming,3.34E+02,Prithvipal Singh
Write in CSV file
package main

import (
	"fmt"
	"os"

	"github.com/Prithvipal/csvparser"
)

type Book struct {
	ID     int     `csv:"id"`
	Title  string  `csv:"book_title"`
	Price  float32 `csv:"price"`
	Author string  `csv:"name_of_author"`
}

func main() {

	file, err := os.Create("books.csv")
	if err != nil {
		fmt.Printf("Error creating file: %v\n", err)
		return
	}
	defer file.Close()
	enc := csvparser.NewEncoder(file)

	b := []Book{
		{
			ID:     101,
			Title:  "Let us C",
			Price:  1232,
			Author: "Auth one",
		},
		{
			ID:     102,
			Title:  "Hands on go programming",
			Price:  334,
			Author: "Prithvipal Singh",
		},
	}

	err = enc.Encode(b)
	if err != nil {
		fmt.Println(err)
	}

}

The following output will be written in books.csv:

id,book_title,price,name_of_author
101,Let us C,1.232E+03,Auth one
102,Hands on go programming,3.34E+02,Prithvipal Singh

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetFieldNameTypeAndValue

func GetFieldNameTypeAndValue(i interface{})

GetFieldNameTypeAndValue ...

func Marshal

func Marshal(i interface{}) [][]string

Marshal ...

Types

type Decoder

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

An Decoder reads cvs values to an input stream.

func NewDecoder

func NewDecoder(r io.Reader) *Decoder

NewDecoder returns a new decoder that reader from r.

type Encoder

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

An Encoder writes cvs values to an output stream.

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

NewEncoder returns a new encoder that writes to w.

func (*Encoder) Encode

func (e *Encoder) Encode(i interface{}) error

Encode method writes cvs encoding

Jump to

Keyboard shortcuts

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