csv

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2021 License: MIT Imports: 6 Imported by: 2

Documentation

Overview

Package csv reads and writes comma-separated values (CSV) files.

You can use flat struct data type to recieve the csv data.

This package are an extended version of encoding/csv package. See encoding/csv package : http://golang.org/pkg/encoding/csv/

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Reader

type Reader struct {
	*csv.Reader
	// contains filtered or unexported fields
}

A Reader reads records from a CSV-encoded file.

Example
package main

import (
	"fmt"
	"strings"
	"time"

	"github.com/atotto/encoding/csv"
)

func main() {

	csvtext := "Gopher,2,12.3,34.5,2020/01/01 12:20:02\nGhost,4,12.3,3.4e-05,2020/02/03 15:20:02"

	type Person struct {
		Name       string
		Age        int
		Height     float32
		Weight     float64
		CreateTime time.Time
	}

	var person = []Person{}

	r := csv.NewReader(strings.NewReader(csvtext))
	r.SetTimeFormat("2006/01/02 15:04:05")
	r.SetTimeLocation(time.UTC)
	err := r.ReadStructAll(&person)
	if err != nil {
		return
	}

	fmt.Printf("%v", person)

}
Output:

[{Gopher 2 12.3 34.5 2020-01-01 12:20:02 +0000 UTC} {Ghost 4 12.3 3.4e-05 2020-02-03 15:20:02 +0000 UTC}]

func NewReader

func NewReader(r io.Reader) *Reader

NewReader returns a new Reader that reads from r.

func (*Reader) ReadStruct

func (r *Reader) ReadStruct(v interface{}) (err error)

func (*Reader) ReadStructAll

func (r *Reader) ReadStructAll(v interface{}) (err error)

func (*Reader) SetTimeFormat

func (r *Reader) SetTimeFormat(format string)

func (*Reader) SetTimeLocation added in v0.1.3

func (r *Reader) SetTimeLocation(location *time.Location)

type UnsupportedTypeError

type UnsupportedTypeError struct {
	Type reflect.Type
}

An UnsupportedTypeError is returned by Writer when attempting to encode an unsupported value type.

func (*UnsupportedTypeError) Error

func (e *UnsupportedTypeError) Error() string

type Writer

type Writer struct {
	*csv.Writer
	// contains filtered or unexported fields
}

A Writer writes records to a CSV encoded file.

see encoding/csv package.

Example
package main

import (
	"os"
	"time"

	"github.com/atotto/encoding/csv"
)

func main() {

	type Person struct {
		Name       string
		Age        int
		Height     float32
		Weight     float64
		CreateTime time.Time
	}

	member := []Person{
		{"Gopher", 2, 12.3, 34.5, time.Date(2020, 1, 1, 12, 20, 2, 0, time.UTC)},
		{"Ghost", 4, 12.3, 0.000034, time.Date(2020, 2, 3, 15, 20, 2, 0, time.UTC)},
	}

	w := csv.NewWriter(os.Stdout)
	w.SetTimeLocation(time.UTC)
	w.WriteStructAll(member)

}
Output:

Gopher,2,12.3,34.5,2020-01-01 12:20:02
Ghost,4,12.3,3.4e-05,2020-02-03 15:20:02

func NewWriter

func NewWriter(w io.Writer) *Writer

NewWriter returns a new Writer that writes to w.

func (*Writer) SetTimeFormat

func (w *Writer) SetTimeFormat(format string)

func (*Writer) SetTimeLocation added in v0.1.3

func (w *Writer) SetTimeLocation(location *time.Location)

func (*Writer) WriteStruct

func (w *Writer) WriteStruct(v interface{}) (err error)

WriteStruct writes a single CSV record to w along with any necessary quoting. A record is a struct of flat structure.

func (*Writer) WriteStructAll

func (w *Writer) WriteStructAll(v interface{}) (err error)

WriteStructAll write

func (*Writer) WriteStructHeader

func (w *Writer) WriteStructHeader(v interface{}) (err error)

Jump to

Keyboard shortcuts

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