csv

package
v0.0.0-...-012d1c6 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2021 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidType      = errors.New("invalid type")
	ErrNoData           = errors.New("no data")
	ErrNotFoundNameLine = errors.New("not found name line")
)

Functions

func Unmarshal

func Unmarshal(data []byte, v interface{}, opts ...Option) error

Unmarshal 根据列头信息反射到数组中 结构体映射分为三种情况: 一:第一行代表字段名,通过字段名映射 二:外部指定每列字段名 三:均没有指定的情况下,按照字段顺序映射 例如: client_id,client_name,client_age int,string,int id,name,age 1,Jose,42 2,Daniel,26 3,Vincent,32

type Client struct { // Our example struct, you can use "-" to ignore a field

	Id      string `csv:"client_id"`
	Name    string `csv:"client_name"`
	Age     string `csv:"client_age"`
	NotUsed string `csv:"-"`
}

clients := []*Client{} Unmarshal(data, &clients)

Types

type Option

type Option func(o *Options)

type Options

type Options struct {
	Comma        rune     // 分隔符,默认逗号
	IgnorePrompt rune     // 忽略该行提示符,默认为#,0表示不忽略
	IgnoreLines  int      // 忽略行数,默认三行,第一行名字,第二行类型,第三行注释
	NameLine     int      // 名字所在行号,默认为0,-1表示没有头信息
	NameHead     []string // 如果数据不提供表头信息,则需要手动提供每一列字段名
}

func (*Options) Init

func (o *Options) Init(opts ...Option)

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