Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Added the Go language with snippets #208

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
AmeerMoustafa wants to merge 2 commits into quicksnip-dev:main from AmeerMoustafa:main
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Added a snippet for writing data to a cvs file
  • Loading branch information
AmeerMoustafa authored and AmeerMoustafa committed Jan 7, 2025
commit ab547f448f2f98c6acbadaa0cb4d5cb2329fd2d9
11 changes: 11 additions & 0 deletions public/consolidated/go.json
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
}
Expand Down
39 changes: 39 additions & 0 deletions snippets/go/file handling/write-data-to-a-cvs-file.md
View file Open in desktop
Copy link
Collaborator

@Mathys-Gasnier Mathys-Gasnier Jan 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not qualify as a snippet, because it is an example

Original file line number Diff line number Diff line change
@@ -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)
}
}
```
Loading

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /