Skip to content

Commit 113488b

Browse files
AmeerMoustafaAmeerMoustafa
authored andcommitted
Added the Go language with a couple of basic snippets
1 parent 34bd82f commit 113488b

File tree

8 files changed

+147
-1
lines changed

8 files changed

+147
-1
lines changed

public/consolidated/_index.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
"icon": "/icons/css.svg",
2020
"subLanguages": []
2121
},
22+
{
23+
"name": "GO",
24+
"icon": "/icons/go.svg",
25+
"subLanguages": []
26+
},
2227
{
2328
"name": "HASKELL",
2429
"icon": "/icons/haskell.svg",

public/consolidated/go.json

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
[
2+
{
3+
"name": "HTTP",
4+
"snippets": [
5+
{
6+
"title": "server",
7+
"description": "runs an HTTP server with routing",
8+
"author": "AmeerMoustafa",
9+
"tags": [
10+
"http",
11+
"server",
12+
"router",
13+
"web"
14+
],
15+
"contributors": [],
16+
"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"
17+
}
18+
]
19+
},
20+
{
21+
"name": "Basics",
22+
"snippets": [
23+
{
24+
"title": "Hello, World!",
25+
"description": "A simple function that outputs \"Hello, World!\"",
26+
"author": "AmeerMoustafa",
27+
"tags": [
28+
"basics",
29+
"formatting"
30+
],
31+
"contributors": [],
32+
"code": "package main\n\nimport \"fmt\"\n\nfunc main() {\n\n fmt.Println(\"Hello, World!\")\n}\n"
33+
}
34+
]
35+
},
36+
{
37+
"name": "File handling",
38+
"snippets": [
39+
{
40+
"title": "Read and write helpers",
41+
"description": "Simple and reusable functions to help with file reading and writing",
42+
"author": "AmeerMoustafa",
43+
"tags": [
44+
"file handling"
45+
],
46+
"contributors": [],
47+
"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"
48+
}
49+
]
50+
}
51+
]

public/consolidated/javascript.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@
410410
"algebra"
411411
],
412412
"contributors": [],
413-
"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"
413+
"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"
414414
},
415415
{
416416
"title": "Cross Product",

public/icons/go.svg

Lines changed: 6 additions & 0 deletions
Loading

snippets/go/HTTP/server.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
title: server
3+
description: runs an HTTP server with routing
4+
author: AmeerMoustafa
5+
tags: http,server,router,web
6+
---
7+
8+
```go
9+
package main
10+
11+
import (
12+
"fmt"
13+
"net/http"
14+
)
15+
16+
var Router = http.NewServeMux()
17+
18+
var server = http.Server{
19+
Addr: ":8080",
20+
Handler: Router,
21+
}
22+
23+
// Handler function example
24+
func example(w http.ResponseWriter, r *http.Request) {
25+
fmt.Fprintln(w, "Hello HTTP server")
26+
}
27+
28+
func main() {
29+
// Route definitions
30+
Router.HandleFunc("GET /", example)
31+
server.ListenAndServe()
32+
}
33+
```

snippets/go/basics/hello-world.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
title: Hello, World!
3+
description: A simple function that outputs "Hello, World!"
4+
author: AmeerMoustafa
5+
tags: basics,formatting
6+
---
7+
8+
```go
9+
package main
10+
11+
import "fmt"
12+
13+
func main() {
14+
15+
fmt.Println("Hello, World!")
16+
}
17+
```
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
title: Read and write helpers
3+
description: Simple and reusable functions to help with file reading and writing
4+
author: AmeerMoustafa
5+
tags: file handling
6+
---
7+
8+
```go
9+
// A reusable function to read data from a file
10+
func readFile(filePath string) (data []byte, err error) {
11+
data, err = os.ReadFile(filePath)
12+
if err != nil {
13+
return nil, fmt.Errorf("failed to read file %s: %w", filePath, err)
14+
}
15+
16+
return data, nil
17+
}
18+
19+
// A reusable function to write data to a file
20+
func writeFile(data string, filePath string) error {
21+
22+
err := os.WriteFile(filePath, []byte(data), 0644)
23+
if err != nil {
24+
return fmt.Errorf("Failed to write to file %s: %w", filePath, err)
25+
}
26+
return nil
27+
}
28+
```

snippets/go/icon.svg

Lines changed: 6 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)