登录
首页 >  Golang >  Go问答

在 golang 中将 Stripe API 的数据写入 CSV 文件

来源:stackoverflow

时间:2024-03-29 20:54:30 439浏览 收藏

欢迎各位小伙伴来到golang学习网,相聚于此都是缘哈哈哈!今天我给大家带来《在 golang 中将 Stripe API 的数据写入 CSV 文件》,这篇文章主要讲到等等知识,如果你对Golang相关的知识非常感兴趣或者正在自学,都可以关注我,我会持续更新相关文章!当然,有什么建议也欢迎在评论留言提出!一起学习!

问题内容

我正在尝试检索 stripe 数据并将其解析为 csv 文件。 这是我的代码:

package main

import (
    "github.com/stripe/stripe-go"
    "github.com/stripe/stripe-go/invoice"
    "fmt"
    "os"
    "encoding/csv"
)

func main() {
    stripe.key = "" // i can't share the api key
    params := &stripe.invoicelistparams{}
    params.filters.addfilter("limit", "", "3")
    params.filters.addfilter("status", "", "paid")
    i := invoice.list(params)

    // create a csv file
    csvdatafile, err := os.create("./mycsvfile.csv")
    if err != nil {
        fmt.println(err)
    }
    defer csvdatafile.close()
    // write unmarshaled json data to csv file
    w := csv.newwriter(csvdatafile)
    //column title
    var header []string
    header = append(header, "id")
    w.write(header)
    for i.next() {
        in := i.invoice()
        fmt.printf(in.id) // it is working
        w.write(in) // it is not working
    }
    w.flush()
    fmt.println("appending succed")
}

当我使用 go run *.go 运行程序时,出现以下错误:

./main.go:35:10: cannot use in (type *stripe.Invoice) as type []string in argument to w.Write

我想我离解决方案已经不远了。 我只需要了解如何在 csv 文件中正确写入,感谢 w.write() 命令。


解决方案


根据文档,write 函数是:

func (w *writer) write(record []string) error

也就是说,它期望您传递代表一行 csv 数据的字符串切片,每个字符串都是一个切片。因此,如果只有一个字段,则必须传递 1 的字符串切片:

w.Write([]string{in.ID})

今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>