I would like to print CSV-data to the output with martini. Currently, I have always used r.JSON(200, somestruct)
where r
is a render.Render
from github.com/martini-contrib
.
Now I have an slice of structs and I would like to print them as CSV (stringify each field of a single struct and print one struct at one line).
Currently, I do it like this:
r.Data(200, []byte("id,Latitude,Longitude\n"))
for _, packet := range tour.Packets {
r.Data(200, []byte(strconv.FormatInt(packet.Id, 10)+","+strconv.FormatFloat(packet.Latitude, 'f', 6, 64)+","+strconv.FormatFloat(packet.Longitude, 'f', 6, 64)+"\n"))
}
But I don't like the way I do it for the following reasons:
- It is downloaded directly and not printed to the screen.
- I get
http: multiple response.WriteHeader calls
- I would prefer not to make this manually (the struct has much more fields, but all fields are either
ìnt64
,float64
ortime.Time
.
How can I implement the CSV export option in a simpler way?