csvutil

package
v0.0.0-...-cd3ace8 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2022 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal

func Marshal(v interface{}) ([]byte, error)

Marshal is encodes a structure or slice of a structure into CSV data.

func MarshalString

func MarshalString(v interface{}) (string, error)

MarshalString is encodes a structure of slice of a structure into CSV string.

Example
package main

import (
	"fmt"

	"go.nanasi880.dev/x/encoding/csvutil"
)

func main() {

	type csvData struct {
		Name   string
		Age    int
		Memo   string `csv:"Comment"`
		Ignore string `csv:"-"`
	}

	d := []csvData{
		{Name: "Bob", Age: 18, Memo: "my name is Bob", Ignore: "Hi"},
		{Name: "Alice", Age: 18, Memo: "my name is Alice", Ignore: "Hi"},
	}

	encoded, err := csvutil.MarshalString(d)
	if err != nil {
		panic(err)
	}

	fmt.Println(encoded)
}
Output:

Name,Age,Comment
Bob,18,my name is Bob
Alice,18,my name is Alice

func Unmarshal

func Unmarshal(csv []byte, out interface{}) error

Unmarshal is decodes a slice of a structure from CSV data.

func UnmarshalString

func UnmarshalString(csv string, out interface{}) error

UnmarshalString is decodes a slice of a structure from CSV string.

Example
package main

import (
	"fmt"

	"go.nanasi880.dev/x/encoding/csvutil"
)

func main() {
	const csvString = `Name,Age,Comment
Bob,18,my name is Bob
Alice,18,my name is Alice`

	type csvData struct {
		Name   string
		Age    int
		Memo   string `csv:"Comment"`
		Ignore int    `csv:"-"`
	}

	var d []csvData
	if err := csvutil.UnmarshalString(csvString, &d); err != nil {
		panic(err)
	}

	fmt.Println(len(d))
	fmt.Println(d[0])
	fmt.Println(d[1])
}
Output:

2
{Bob 18 my name is Bob 0}
{Alice 18 my name is Alice 0}

Types

type Decoder

type Decoder struct {
	Comma            rune
	Comment          rune
	FieldsPerRecord  int
	LazyQuotes       bool
	TrimLeadingSpace bool
	ReuseRecord      bool
	UseHeader        bool
	Nil              string
	// contains filtered or unexported fields
}

Decoder reads CSV values to an input stream.

func NewDecoder

func NewDecoder(r io.Reader) *Decoder

NewDecoder is create csv decoder.

func (*Decoder) Decode

func (d *Decoder) Decode(out interface{}) error

Decode is decodes a slice of a structure from CSV data, CSV data read from the io.Reader specified by NewDecoder.

type Encoder

type Encoder struct {
	Comma     rune
	UseCRLF   bool
	UseHeader bool
	Nil       string
	// contains filtered or unexported fields
}

An Encoder writes CSV values to an output stream.

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

NewEncoder is create csv encoder.

func (*Encoder) Encode

func (e *Encoder) Encode(v interface{}) (err error)

Encode is encodes a structure or slice of a structure into CSV data and output to the io.Writer specified by NewEncoder.

Example
package main

import (
	"os"

	"go.nanasi880.dev/x/encoding/csvutil"
)

func main() {

	type csvData struct {
		Name   string
		Age    int
		Memo   string `csv:"Comment"`
		Ignore string `csv:"-"`
	}

	d := []csvData{
		{Name: "Bob", Age: 18, Memo: "my name is Bob", Ignore: "Hi"},
		{Name: "Alice", Age: 18, Memo: "my name is Alice", Ignore: "Hi"},
	}

	err := csvutil.NewEncoder(os.Stdout).Encode(d)
	if err != nil {
		panic(err)
	}
}
Output:

Name,Age,Comment
Bob,18,my name is Bob
Alice,18,my name is Alice

type Marshaler

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

Marshaler is the interface implemented by types that can marshal themselves into valid CSV.

type Unmarshaler

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

Unmarshaler is the interface implemented by types that can unmarshal a CSV description of themselves. The input can be assumed to be a valid encoding of a CSV value. UnmarshalCSV must copy the CSV data if it wishes to retain the data after returning.

Jump to

Keyboard shortcuts

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