iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >Go CSV包如何实现结构体和csv内容互转工具
  • 355
分享到

Go CSV包如何实现结构体和csv内容互转工具

2023-07-05 12:07:38 355人浏览 独家记忆
摘要

这篇文章主要讲解了“Go CSV包如何实现结构体和csv内容互转工具”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Go CSV包如何实现结构体和csv内容互转工具”吧!g

这篇文章主要讲解了“Go CSV包如何实现结构体和csv内容互转工具”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Go CSV包如何实现结构体和csv内容互转工具”吧!

gocsv的基本功能

gocsv包的最基本的作用就是能够方便的将csv内容转换到对应的结构体上,或者将结构体的内容快速的转换成csv格式(包括写入文件)。

Go CSV包如何实现结构体和csv内容互转工具

gocsv.UnmarshalFile函数:csv内容转成结构体

假设文件中的内容如下:

client_id,client_name,client_age1,Jose,422,Daniel,263,Vincent,32

然后从文件中读取出内容,并直接转换到结构体Client上,如下:

package mainimport ("fmt""os""GitHub.com/gocarina/gocsv")type NotUsed struct {Name string}type Client struct { // Our example struct, you can use "-" to ignore a fieldId            string `csv:"client_id"`Name          string `csv:"client_name"`Age           string `csv:"client_age"`NotUsedString string `csv:"-"`NotUsedStruct NotUsed `csv:"-"` }func main() {clientsFile, err := os.OpenFile("clients.csv", os.O_RDWR|os.O_CREATE, os.ModePerm)if err != nil {panic(err)}defer clientsFile.Close()clients := []*Client{}if err := gocsv.UnmarshalFile(clientsFile, &clients); err != nil { // Load clients from filepanic(err)}for _, client := range clients {fmt.Println("Hello", client.Name)}}

gocsv.MarshalFile函数:结构体转成csv文件

package mainimport ("fmt""os""github.com/gocarina/gocsv")type NotUsed struct {Name string}type Client struct { // Our example struct, you can use "-" to ignore a fieldId            string `csv:"client_id"`Name          string `csv:"client_name"`Age           string `csv:"client_age"`NotUsedString string `csv:"-"`NotUsedStruct NotUsed `csv:"-"` }func main() {clientsFile, err := os.OpenFile("clients.csv", os.O_RDWR|os.O_CREATE, os.ModePerm)if err != nil {panic(err)}defer clientsFile.Close()clients := []*Client{}clients = append(clients, &Client{Id: "12", Name: "John", Age: "21"}) // Add clientsclients = append(clients, &Client{Id: "13", Name: "Fred"})clients = append(clients, &Client{Id: "14", Name: "James", Age: "32"})clients = append(clients, &Client{Id: "15", Name: "Danny"})err = gocsv.MarshalFile(&clients, clientsFile) // Use this to save the CSV back to the fileif err != nil {panic(err)}}

自定义类型转换器

gocsv包还可以给自定义的结构体类型定义csv和结构体的互转函数。只要自定义的类型实现如下接口即可:

type TypeMarshaller interface {MarshalCSV() (string, error)}// TypeUnmarshaller is implemented by any value that has an UnmarshalCSV method// This converter is used to convert a string to your value representation of that stringtype TypeUnmarshaller interface {UnmarshalCSV(string) error}

或者将结构体转换成csv字符串时,需要实现如下接口:

// MarshalText encodes the receiver into UTF-8-encoded text and returns the result.type TextMarshaler interface {MarshalText() (text []byte, err error)}type TextUnmarshaler interface {UnmarshalText(text []byte) error}

例如,我们定义了一个结构体DateTime,里面有一个time.Time类型的属性。并且DateTime类型实现了TypeMarshaller接口的MarshalCSV函数和TypeUnmarshaller接口的UnmarshalCSV函数。如下:

type DateTime struct {time.Time}// Convert the internal date as CSV stringfunc (date *DateTime) MarshalCSV() (string, error) {return date.Time.FORMat("20060201"), nil}// You could also use the standard Stringer interface func (date *DateTime) String() (string) {return date.String() // Redundant, just for example}// Convert the CSV string as internal datefunc (date *DateTime) UnmarshalCSV(csv string) (err error) {date.Time, err = time.Parse("20060201", csv)return err}type Client struct { // Our example struct with a custom type (DateTime)Id       string   `csv:"id"`Name     string   `csv:"name"`Employed DateTime `csv:"employed"`}func main() {client := []Client{{Id: "001",Name: "Go学堂",Employed: DateTime{time.Now()},},}csvContent, _ := gocsv.MarshalString(client)fmt.Println("csv:", csvContent) //输出内容是 001,Go学堂,20231003}

当我们运行上述代码,最终的输出内容是:

001,Go学堂,20231003

最后的日期就是按DateTime的MarshalCSV函数格式输出的。

自定义CSV的Reader/Writer

在开头处我们提到,csv文件中的分隔符默认是逗号。但也可以是其他字符。这就要求我们在读取或写入之前指定好内容的分隔号。那么就可以通过自定义的Reader/Writer来覆盖默认的Reader/Writer的选项。如下:

  • 指定读取内容的分割符是 "|"

gocsv.SetCSVReader(func(in io.Reader) gocsv.CSVReader {    r := csv.NewReader(in)r.Comma = '|'    return r // Allows use pipe as delimiter})
  • 指定写入的内容是用 分割符 "|" 进行分割的

gocsv.SetCSVWriter(func(out io.Writer) *gocsv.SafeCSVWriter {writer := csv.NewWriter(out)    writer.Comma = '|'    return gocsv.NewSafeCSVWriter(writer)})

gocsv包的特点总结

结构体切片和csv内容互转。能够将结构体切片(或数组)直接输出成csv内容或输出到文件。反之亦然。

csv标签。其转换过程是通过结构体上的“csv”标签进行关联的。

csv标签对应csv内容表头。当结构体和csv格式互转时,结构体中的csv标签对应的就是csv表格的表头,结构体中的字段顺序对应的就是csv文件列的顺序。

底层依然是使用标准库中的csv。在写入csv文件时,底层实际上用的还是go标准库中的encoding/csv/Writer结构体的Write(row []string)方法。

自动将结构体字段的类型转换成字符串:大家看到标准csv包中的Write方法的入参是string类型的切片,而在要转换的结构体上的字段可以是各种类型。这里就是gocsv包中的一个特点:可以将字段中的非string类型转换成string类型,最终写入到csv文件中。

可自定义类型转换器。可以通过实现TypeMarshaller接口或TypeUnMarshaller接口对自定义类型的内容按对应的格式输出成csv内容。

可自定义CSV的Reader/Writer来覆盖默认参数。比如csv格式的内容默认使用逗号分隔内容。通过该功能我们可以指定使用其他分隔符的csv内容。比如使用"|"或";"等。

这里需要注意的是 将csv文件的内容一定是解析到结构体类型的切片或数组中。同样,也只有是结构体类型的切片或数组才能直接写入到csv文件中。

感谢各位的阅读,以上就是“Go CSV包如何实现结构体和csv内容互转工具”的内容了,经过本文的学习后,相信大家对Go CSV包如何实现结构体和csv内容互转工具这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是编程网,小编将为大家推送更多相关知识点的文章,欢迎关注!

--结束END--

本文标题: Go CSV包如何实现结构体和csv内容互转工具

本文链接: https://www.lsjlt.com/news/351577.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Go CSV包如何实现结构体和csv内容互转工具
    这篇文章主要讲解了“Go CSV包如何实现结构体和csv内容互转工具”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Go CSV包如何实现结构体和csv内容互转工具”吧!g...
    99+
    2023-07-05
  • go语言string转结构体如何实现
    这篇“go语言string转结构体如何实现”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“go语言string转结构体如何实现...
    99+
    2023-07-05
  • Go语言strconv包如何实现字符串和数值类型的相互转换
    这篇文章将为大家详细讲解有关Go语言strconv包如何实现字符串和数值类型的相互转换,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。在实际开发中我们往往需要对一些常用的数据类型进行转换,如 string、...
    99+
    2023-06-14
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作