A CSV library is only half done if it cannot write. Today you build the writer's core, turning a table of records back into text by joining fields with the delimiter and ending each record with a newline.
Serialize a table of plain records to CSV text with comma delimiters and newline terminators.
Reading and writing are two directions of the same format, and now that the reader is complete you build its mirror. The writer’s core is almost trivial for plain data: join each record’s fields with the delimiter and end each record with a line terminator. Choose the newline as the default terminator, which keeps the output compact and makes it round-trip cleanly with your parser; the RFC prefers a carriage return plus newline, and offering that as a dialect option is a natural extension later.
The one decision to make deliberately is that every record, including the last,
ends with a terminator. That means the output of a single record is x followed by a
newline, not a bare x. Terminating every record is what lets your parser read the
result back without a special case for the final line, and it matches the earlier
rule that a trailing newline does not create a phantom empty record. Keep today’s
writer assuming plain fields with no commas, quotes, or newlines in them; the next
lesson adds the quoting that makes the writer safe for any content, and the lesson
after proves the two directions are exact inverses.
// join each record's fields with the delimiter, terminate each record with '\n'func Write(records [][]string) string {// for each record: strings.Join(fields, ",") + "\n"// (quoting comes next lesson; assume plain fields today)}