-
-
Notifications
You must be signed in to change notification settings - Fork 130
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
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
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
commit 18487b276eaba84082891c93dc6d070b0f28c79c
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
[γγ¬γΌγ ]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
[γγ¬γΌγ ]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
[γγ¬γΌγ ]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
[γγ¬γΌγ ]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
[γγ¬γΌγ ]
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!") | ||
} | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
``` |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
[γγ¬γΌγ ]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
*/ | ||
``` | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.