golang 解析csv文件到struct

golang 解析csv文件到struct

install

go get github.com/zhnxin/csvreader

usage

简单用法

Note

默认情况下,csv 文件的首行会被当作header处理。

file.csv

hosname,ip
redis,172.17.0.2
mariadb,172.17.0.3

go

type Info struct{
    Hostname string
    IP string
}

//struct slice
infos := []Info{}
_ = csvreader.New().UnMarshalFile("file.csv",&infos)
body,_ := json.Marshal(infos)
fmt.Println(string(body))

//point slice
infos = []*Info{}
_ = csvreader.New().UnMarshalFile("file.csv",&infos)
body,_ := json.Marshal(infos)
fmt.Println(string(body))

Note

如果 csv 文件首行不包含header,可以使用 WithHeader([]string) 来指定header。
_ = csvreader.New().WithHeader([]string{"hostname","ip"}).UnMarshalFile("file.csv",&infos)

自定义parster

就像枚举类型(enum),偶尔会遇到这种需要实现自定义转换过程的情况。例子如下

type NetProtocol uint32
const(
    NetProtocol_TCP NetProtocol = iota
    NetProtocol_UDP
    NetProtocol_DCCP
    NetProtocol_SCTP
)

type ServiceInfo struct{
    Host string
    Port string
    Protocol NetProtocol
}

直接使用原始的类型来编辑csv文件,十分不便。这时就需要实现自定义parser。

Tip

type CsvMarshal interface {
    FromString(string) error
}
func (p *NetProtocol)FromString(protocol string) error{
    switch strings.ToLower(protocol){
        case "tcp":
            *p = NetProtocol_TCP
        case "udp":
            *p = NetProtocol_UDP
        case "dccp":
            *p = NetProtocol_DCCP
        case "sctp":
            *p = NetProtocol_SCTP
        default:
            return fmt.Errorf("unknown protocoal:%s",protocol)
    }
    return nil
}

另外一个例子 TestCustom

转载于:https://my.oschina.net/u/3703365/blog/3058322

猜你喜欢

转载自blog.csdn.net/weixin_34101784/article/details/92400802