从csv文件中的查询gorm写入struct类型的数组
我有这样一个类型结构:从csv文件中的查询gorm写入struct类型的数组,csv,go,Csv,Go,我有这样一个类型结构: type ExtParticipants struct { ParticipantId int `gorm:"PRIMARY_KEY"` ParticipantData time.Time EventId int UserId int } 例如,如果我要获取某个事件中的所有参与者,我将执行以下查询: func GetParticipants(db *gorm.DB, eventId int) ([]*ExtParticipants,
type ExtParticipants struct {
ParticipantId int `gorm:"PRIMARY_KEY"`
ParticipantData time.Time
EventId int
UserId int
}
例如,如果我要获取某个事件中的所有参与者,我将执行以下查询:
func GetParticipants(db *gorm.DB, eventId int) ([]*ExtParticipants, error) {
var participants []*ExtParticipants
query := db.Table("ext_participants").Where("event_id = ?", eventId).Find(&participants)
return participants, query.Error
}
是否有本机方法将该查询的输出写入csv(一个structs ExtParticipants数组)
从库“encoding/csv”中,有以下方法:
func (w *Writer) Write(record []string) error
func (w *Writer) WriteAll(records [][]string) error
它只接受字符串数组或矩阵作为输入,但如果有一种类似的方法接受结构作为输入,并将同一结构的字段名称作为标题写入,那将是理想的选择。您尝试过吗?我刚刚在尝试,谢谢!