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.
You need to call writer.Flush()
before calling .Bytes()
and check .Error()
before returning:
// TODO: remove `defer writer.Flush()`
// ...
writer.Flush()
if err := writer.Error(); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
Your sample code does things in the following order:
buf.Bytes()
writer.Flush()
Clearly this is not the intended order since we need to flush (and check for any writer errors!) before accessing the generated byte slice.