Documentation ¶
Overview ¶
Package cswizard is a CSV writer that doesn't stand in your way as your system evolves. Using `encoding/csv` directly is fine when you want to just do the thing and forget it. Long-living projects, however, are rarely done this way: every day business demands to add new columns, remove and reorder them, and under this conditions `encoding/csv` becomes too fragile due to nature of its API: you just can't change one thing and be sure that everything else would keep working. With CSWizard, you can.
So, in a nutshell, that's a small wrapper around `encoding/csv` for reports that change often in various ways.
A typical usage would be something like that:
cw := csv.NewWriter(os.Stdout) w := cswizard.New(cw) colName := w.AddHeader("Client name") colAge := w.AddHeader("Client age") colHeight := w.AddHeader("Client height (predicted)") err := w.LockHeaders() if err != nil { return } for _, c := range clients { row := w.CreateRow() row[colName] = c.Name row[colAge] = strconv.FormatUint(c.Age, 10) row[colHeight] = strconv.FormatUint(c.Height, 10) err := w.CommitRow(row) if err != nil { return } } cw.Flush()
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Writer ¶
type Writer interface { // AddHeader adds a new header and returns its index which you can // use when you'll be filling a row. AddHeader(string) (id uint64) // LockHeaders writes all headers added before into CSV and locks // header list. You must call LockHeaders before you can start adding // the rows. Returns an error (if any). LockHeaders() error // CreateRow creates a new row and returns a buffer which you can populate // using indexes you've got from AddHeader calls, and later commit with // CommitRow. // // Internally, this buffer is reused between calls. This may change // in the future. CreateRow() []string // CommitRow accepts a buffer of columns (which you normally obtain // from CreateRow) and adds it into CSV. Returns an error (if any). CommitRow([]string) error }
Writer provides methods for composing a CSV.