I have created a function to get some data and write them to CSV and the output is stored on a buffer.
type OptIn struct {
Email string `json:"email"`
LastUpdate string `json:"opt_in_last_update"`
}
func writeCSV(data []OptIn) ([]byte, error) {
var buf bytes.Buffer
writer := csv.NewWriter(&buf)
defer writer.Flush()
for _, obj := range data {
var record []string
record = append(record, obj.Email)
record = append(record, obj.LastUpdate)
err := writer.Write(record)
if err != nil {
panic(err.Error())
}
}
return buf.Bytes(), nil
}
The problem is that the buf.Bytes()
is always empty, even though the input is not empty and there are no errors thrown.