diff --git a/public/consolidated/go.json b/public/consolidated/go.json index 6636aea8..431d38f1 100644 --- a/public/consolidated/go.json +++ b/public/consolidated/go.json @@ -45,6 +45,17 @@ ], "contributors": [], "code": "// A reusable function to read data from a file\nfunc readFile(filePath string) (data []byte, err error) {\n\tdata, err = os.ReadFile(filePath)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to read file %s: %w\", filePath, err)\n\t}\n\n\treturn data, nil\n}\n\n// A reusable function to write data to a file\nfunc writeFile(data string, filePath string) error {\n\n\terr := os.WriteFile(filePath, []byte(data), 0644)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"Failed to write to file %s: %w\", filePath, err)\n\t}\n\treturn nil\n}\n" + }, + { + "title": "Write data to a CVS file", + "description": "An example of writing data to a cvs file", + "author": "AmeerMoustafa", + "tags": [ + "file handling", + "cvs" + ], + "contributors": [], + "code": "package main\n\nimport (\n \"os\"\n \"log\"\n \"encoding/csv\"\n)\n\nvar data = [][]string{{\"Line1\", \"Hello Readers of\"}, {\"Line2\", \"quicksnip.dev\"}}\n\nfunc main() {\n file, err := os.Create(\"result.csv\")\n checkError(\"Cannot create file\", err)\n defer file.Close()\n\n writer := csv.NewWriter(file)\n defer writer.Flush()\n\n for _, value := range data {\n err := writer.Write(value)\n checkError(\"Cannot write to file\", err)\n }\n}\n\nfunc checkError(message string, err error) {\n if err != nil {\n log.Fatal(message, err)\n }\n}\n" } ] } diff --git a/snippets/go/file handling/write-data-to-a-cvs-file.md b/snippets/go/file handling/write-data-to-a-cvs-file.md new file mode 100644 index 00000000..b1bb295b --- /dev/null +++ b/snippets/go/file handling/write-data-to-a-cvs-file.md @@ -0,0 +1,39 @@ +--- +title: Write data to a CVS file +description: An example of writing data to a cvs file +author: AmeerMoustafa +tags: file handling,cvs +--- + + +```go +package main + +import ( + "os" + "log" + "encoding/csv" +) + +var data = [][]string{{"Line1", "Hello Readers of"}, {"Line2", "quicksnip.dev"}} + +func main() { + file, err := os.Create("result.csv") + checkError("Cannot create file", err) + defer file.Close() + + writer := csv.NewWriter(file) + defer writer.Flush() + + for _, value := range data { + err := writer.Write(value) + checkError("Cannot write to file", err) + } +} + +func checkError(message string, err error) { + if err != nil { + log.Fatal(message, err) + } +} +``` \ No newline at end of file