Write data to a CSV file
·
211 words
·
1 minute read
CSV is a highly accepted data language, commonly used by Excel and spreadsheets, and as such is very useful if your script is producing data and you want it in a common format.
In the example, we’re forming a data variable, just to illustrate the example, by creating a multi-dimensional array of strings. We are then having to create a file (result.csv) for the csv writer to work with. Finally, we are iterating through the data writing each line to the file.
|
|
The result:
Line1,Hello Readers of
Line2,golangcode.com
This process is possible through two packages, os
and encoding/csv
- one which handles the file interactions the other which converts the data structure into csv format.