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 Go language with 5 example snippets #265

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
abdiToldSo wants to merge 2 commits into quicksnip-dev:main from abdiToldSo: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
Next Next commit
added go code snippets
  • Loading branch information
technoph1le authored and abdiToldSo committed Feb 28, 2025
commit 18487b276eaba84082891c93dc6d070b0f28c79c
Binary file modified public/favicon/android-chrome-192x192.png
View file Open in desktop
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
[フレーム]
Binary file modified public/favicon/android-chrome-512x512.png
View file Open in desktop
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
[フレーム]
Binary file modified public/favicon/apple-touch-icon.png
View file Open in desktop
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
[フレーム]
Binary file modified public/favicon/favicon-16x16.png
View file Open in desktop
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
[フレーム]
Binary file modified public/favicon/favicon-32x32.png
View file Open in desktop
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
[フレーム]
Binary file modified public/favicon/favicon.ico
View file Open in desktop
Binary file not shown.
16 changes: 16 additions & 0 deletions snippets/go/basics/hello-world.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
title: Hello, World!
description: Prints Hello, World! to the terminal.
author: abdiToldSo
tags: printing, hello-world, fmt
---

```go
package main //Declare package as main

import "fmt" //Import standard I/O package

func main() {
fmt.Println("Hello world!")
}
```
38 changes: 38 additions & 0 deletions snippets/go/error-handling/error-handling.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
title: Error Handling
description: Calls a function that returns an error and prints it
author: abdiToldSo
tags: error-handling
---

```go
package main

import (
"errors" // Standard Error Library
"math" // Standard Math Library
"fmt" // Standard IO Library
)

func returnErr(x float64) (float64, error) {
if ( x < 0 ) { // Check if input is negative
// Return new error from function
return -1, errors.New("Error: Negative number has no real square root!")
} else {
// Else return square root and nil error
return math.Sqrt(x), nil
}
}

// Usage:
func main() {
result, err := doStuff(-10) // Will return error with result = -1
// If function returns error, print error
if err != nil {
fmt.Println(err)
// Else return result
} else {
fmt.Println(result)
}
}
```
60 changes: 60 additions & 0 deletions snippets/go/icon.svg
View file Open in desktop
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
[フレーム]
37 changes: 37 additions & 0 deletions snippets/go/server/basic-web-server.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
title: Basic Web Server
description: Creates a server with simple routing to display a lovely message.
author: abdiToldSo
tags: net-http, web-server
---

```go
package main

import (
"fmt" // Standard IO
"log" // Standard Log Library
"net/http" // Standard RESTFUL HTTP Client Library
)

func handler(w http.ResponseWriter, r *http.Request) {
// Print basic string to server
fmt.Fprintf(w, "Say you love someone by adding '/name' to the url")
}

func loveHandler(w http.ResponseWriter, r *http.Request) {
// Parse name from router
name := r.PathValue("name")
// Output lovely message for name to server
w.Write([]byte("Hi, I love " + name + "!"))
}

// Usage:
func main() {
// Create webserver & handle root address with 'handler' function
http.HandleFunc("/", handler)
http.HandleFunc("/{name}", loveHandler)
// Start server on port 80801 & 'nil' has root handler
log.Fatal(http.ListenAndServe(":8081", nil))
}
```
51 changes: 51 additions & 0 deletions snippets/go/structs/structs.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
title: Structs
description: Creates two people, calculates their distances from each other, & prints output.
author: abdiToldSo
tags: structs
---

```go
package main

import (
"fmt"
"math"
)

type Person struct {
Name string
Age int
Pos Position
}

type Position struct {
X, Y float64
}


// Struct Method
func (p *Person) distanceFrom(p1 *Person) (float64, float64) {
x_distance := math.Abs(p.Pos.X - p1.Pos.X) // Get absolute distance from self.X to other.X
y_distance := math.Abs(p.Pos.Y - p1.Pos.Y) // Get absolute distance from self.Y to other.Y
return x_distance, y_distance // Return results
}

// Usage:
func main() {
var Dave = Person{"Dave", 27, Position{255, 197}}
Ashley := Person{"Ashley", 28, Position{54, 225}}

// Calculate Distance from other Person
x_distance, y_distance := Dave.distanceFrom(&Ashley)

// Format string's to output X & Y distances
x_string := fmt.Sprintf("%s's X distance from %s: %f", Ashley.Name, Dave.Name, x_distance)
y_string := fmt.Sprintf("%s's Y distance from %s: %f", Ashley.Name, Dave.Name, y_distance)

// Output distance's
fmt.Println(x_string)
fmt.Println(y_string)

}
```
49 changes: 49 additions & 0 deletions snippets/go/templates/basic-html-templating.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
title: Basic HTML Templating
description: Modifies html elements using go struct object
author: abdiToldSo
tags: html, web-server, templates
---

```go
package main

import (
"fmt" // Standard IO library
"html/template" // Standard templating library
"log" // Standard logging library
"net/http" // Standard RESTful HTTP linrary
)

// Create a Post struct
type Post struct {
Title string
Content string
}

// Home Route Handler
func homeHandler(w http.ResponseWriter, r *http.Request) {
p := Post{Title: "Good News", Content: "I'm made 12 dablones yesterday :)"}
t, err := template.ParseFiles("index.html")
if err != nil { log.Fatal(err) }
t.Execute(w, p) // <- Can be called multiple times
}

// Usage:
func main() {
http.HandleFunc("/", homeHandler)
err := http.ListenAndServe(":8081", nil)
if err != nil { log.Fatal(err) }
}

/*
Rules:
L Dynamic content is defined using the following syntax:
{{ .VariableName }}
-------
index.html
<h1>{{ .Title }}</h1>
<h2>{{ .Content }}</h2>
*/
```

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