Skip to content

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
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions public/consolidated/_index.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
"icon": "/icons/css.svg",
"subLanguages": []
},
{
"name": "GO",
"icon": "/icons/go.svg",
"subLanguages": []
},
{
"name": "HASKELL",
"icon": "/icons/haskell.svg",
Expand Down
62 changes: 62 additions & 0 deletions public/consolidated/go.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
[
{
"name": "HTTP",
"snippets": [
{
"title": "server",
"description": "runs an HTTP server with routing",
"author": "AmeerMoustafa",
"tags": [
"http",
"server",
"router",
"web"
],
"contributors": [],
"code": "package main\n\nimport (\n\t\"fmt\"\n\t\"net/http\"\n)\n\nvar Router = http.NewServeMux()\n\nvar server = http.Server{\n\tAddr: \":8080\",\n\tHandler: Router,\n}\n\n// Handler function example\nfunc example(w http.ResponseWriter, r *http.Request) {\n\tfmt.Fprintln(w, \"Hello HTTP server\")\n}\n\nfunc main() {\n\t// Route definitions\n\tRouter.HandleFunc(\"GET /\", example)\n\tserver.ListenAndServe()\n}\n"
}
]
},
{
"name": "Basics",
"snippets": [
{
"title": "Hello, World!",
"description": "A simple function that outputs \"Hello, World!\"",
"author": "AmeerMoustafa",
"tags": [
"basics",
"formatting"
],
"contributors": [],
"code": "package main\n\nimport \"fmt\"\n\nfunc main() {\n\n fmt.Println(\"Hello, World!\")\n}\n"
}
]
},
{
"name": "File handling",
"snippets": [
{
"title": "Read and write helpers",
"description": "Simple and reusable functions to help with file reading and writing",
"author": "AmeerMoustafa",
"tags": [
"file handling"
],
"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"
}
]
}
]
2 changes: 1 addition & 1 deletion public/consolidated/javascript.json
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@
"algebra"
],
"contributors": [],
"code": "function combinations(n, r) {\n function factorial(x) {\n if (x === 0 || x === 1) return 1;\n let result = 1;\n for (let i = 2; i <= x; i++) {\n result *= i;\n }\n return result;\n }\n return factorial(n) / (factorial(r) * factorial(n - r));\n}\n\n// Usage:\ncombinations(12,24); // Returns: 7.720248753351544e-16\ncombinations(1,22); // Returns: 8.896791392450574e-22\n"
"code": "function combinations(n, r) {\n if (n < 0 || r < 0 || n < r) {\n throw new Error('Invalid input: n and r must be non-negative and n must be greater than or equal to r.');\n }\n\n function factorial(x) {\n if (x === 0 || x === 1) return 1;\n let result = 1;\n for (let i = 2; i <= x; i++) {\n result *= i;\n }\n return result;\n }\n\n const numerator = factorial(n);\n const denominator = factorial(r) * factorial(n - r);\n return numerator / denominator;\n}\n\n// Usage:\ncombinations(24,22); // Returns: 276\ncombinations(5,3); // Returns: 10\n"
},
{
"title": "Cross Product",
Expand Down
6 changes: 6 additions & 0 deletions public/icons/go.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions snippets/go/HTTP/server.md
Copy link
Collaborator

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,33 @@
---
title: server
description: runs an HTTP server with routing
author: AmeerMoustafa
tags: http,server,router,web
---

```go
package main

import (
"fmt"
"net/http"
)

var Router = http.NewServeMux()

var server = http.Server{
Addr: ":8080",
Handler: Router,
}

// Handler function example
func example(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello HTTP server")
}

func main() {
// Route definitions
Router.HandleFunc("GET /", example)
server.ListenAndServe()
}
```
17 changes: 17 additions & 0 deletions snippets/go/basics/hello-world.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
title: Hello, World!
description: A simple function that outputs "Hello, World!"
author: AmeerMoustafa
tags: basics,formatting
---

```go
package main

import "fmt"

func main() {

fmt.Println("Hello, World!")
}
```
28 changes: 28 additions & 0 deletions snippets/go/file handling/read-and-write-helpers.md
Copy link
Collaborator

Choose a reason for hiding this comment

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

Those snippets only wrap around os.ReadFile and os.WriteFile without adding anything really usefull, as such, they don't qualify as snippets under our guidelines

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
title: Read and write helpers
description: Simple and reusable functions to help with file reading and writing
author: AmeerMoustafa
tags: file handling
---

```go
// A reusable function to read data from a file
func readFile(filePath string) (data []byte, err error) {
data, err = os.ReadFile(filePath)
if err != nil {
return nil, fmt.Errorf("failed to read file %s: %w", filePath, err)
}

return data, nil
}

// A reusable function to write data to a file
func writeFile(data string, filePath string) error {

err := os.WriteFile(filePath, []byte(data), 0644)
if err != nil {
return fmt.Errorf("Failed to write to file %s: %w", filePath, err)
}
return nil
}
```
39 changes: 39 additions & 0 deletions snippets/go/file handling/write-data-to-a-cvs-file.md
Copy link
Collaborator

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)
}
}
```
6 changes: 6 additions & 0 deletions snippets/go/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading