gocsv

作者: 次序 | 来源:发表于2020-07-19 11:12 被阅读0次
package main

import (
    "encoding/json"
    "fmt"
    "io/ioutil"

    "github.com/gocarina/gocsv"
)

type AchieveConf struct {
    ID         string `csv:"id"`
    Name       string `csv:"nameutf"`
    Clothes    string `csv:"clothes"`
    ClothesVec []int  `csv:"-"`
}

func main() {
    b, err := ioutil.ReadFile("data.csv")
    if err != nil {
        panic(err)
    }

    achs := []*AchieveConf{}
    if err := gocsv.UnmarshalBytes(b, &achs); err != nil {
        panic(err)
    }

    var tachs []*AchieveConf
    for _, ach := range achs {
        if ach.Clothes != "" {
            tachs = append(tachs, ach)
            if err := json.Unmarshal([]byte(ach.Clothes), &ach.ClothesVec); err != nil {
                panic(err)
            }
        }
    }

    achs = tachs
    for _, ach := range achs {
        fmt.Println(ach)
    }
}

相关文章

网友评论

      本文标题:gocsv

      本文链接:https://www.haomeiwen.com/subject/xoelkktx.html