Skip to content

Instantly share code, notes, and snippets.

@hnabbasi
Last active August 10, 2022 16:08
Show Gist options
  • Save hnabbasi/6ca06093ded18143a80350a123e06424 to your computer and use it in GitHub Desktop.
Save hnabbasi/6ca06093ded18143a80350a123e06424 to your computer and use it in GitHub Desktop.
//
// GO Snippets
//
// Written by Hussain Abbasi for learning purpose.
//
// Feedback or question? Reach out Twitter @HussainNAbbasi or GitHub @hnabbasi
//
// Use this code in any shape or form as you please 😊
//
package main
import (
"fmt"
"math"
"os"
"strconv"
"sync"
)
func main() {
varRunner()
arrayRunner()
mapRunner()
loopRunner()
typeRunner()
genericsRunner()
fibRunner()
goroutinesRunner()
channelsRunner()
interfaceRunner()
}
//basic
func varRunner() {
var fName string
fName = "Hussain"
fmt.Printf("Hi %s!\n", fName)
var lName = "Abbasi"
fmt.Printf("Hi %s %s!\n", fName, lName)
age := 30
fmt.Printf("%v!!? Wow. You are old!\n", age)
}
//array
func arrayRunner() {
states := []string{"AR", "MO", "TX"}
for i, s := range states {
fmt.Printf("%v:%s\n", i, s)
}
fmt.Printf("Length of array: %d\n", len(states))
fmt.Printf("Capacity of array: %d\n", cap(states))
}
//map
func mapRunner() {
states := make(map[string]int)
states["AR"] = 7_174_857
states["MO"] = 6_124_635
states["TX"] = 28_635_482
for s, p := range states {
fmt.Printf("The population of %s is %d\n", s, p)
}
fmt.Printf("Length/Size of map: %d\n", len(states))
}
//flow control
func loopRunner() {
current := 0
for {
if current < 10 {
fmt.Printf("Current: %d\n", current)
current++
continue
}
break
}
}
//types
type Vehicle struct {
Make string
Model string
}
type Car struct {
Vehicle
Speed int
}
type Truck struct {
Vehicle
TowCapacity int
}
func (t Truck) Load(weight int) string {
if weight > t.TowCapacity {
return "Capacity overload"
}
return "Loaded"
}
func typeRunner() {
car := Car{Vehicle: Vehicle{Make: "Audi", Model: "R8"}, Speed: 160}
fmt.Println(car)
truck := Truck{Vehicle: Vehicle{Make: "Ford", Model: "F150"}, TowCapacity: 10000}
fmt.Println(truck)
status := truck.Load(10000)
fmt.Println(status)
}
//fib
func fibRunner() {
var limit int
var err error
if len(os.Args) < 2 {
limit = 10
} else if limit, err = strconv.Atoi(os.Args[1]); err != nil {
fmt.Errorf("Provided limit is not in correct format %s", err.Error())
}
result := fib(limit)
fmt.Printf("Fib for %v is %v\n", limit, result)
cache := make(map[int]int)
result = fibFast(limit, cache)
fmt.Printf("FAST Fib for %v is %v\n", limit, result)
}
func fib(n int) int {
if n <= 1 {
return n
}
return fib(n-1) + fib(n-2)
}
func fibFast(n int, cache map[int]int) int {
if val, ok := cache[n]; ok {
return val
}
if n < 2 {
cache[n] = n
return n
}
result := fibFast(n-1, cache) + fibFast(n-2, cache)
cache[n] = result
return result
}
//goroutines
func goroutinesRunner() {
wg := sync.WaitGroup{}
wg.Add(3)
go func() {
sayHi("Hussain")
wg.Done()
}()
go func() {
sayHi("Nasir")
wg.Done()
}()
go func() {
sayHi("Abbasi")
wg.Done()
}()
wg.Wait()
}
//channels
func channelsRunner() {
c := make(chan string)
go func(c chan string) {
c <- "Hello"
}(c)
fmt.Println(<-c)
}
//generics
func genericsRunner() {
a, b := 3, 4
fmt.Printf("%d + %d = %d\n", a, b, add(a, b))
fmt.Printf("%d + %d = %d\n", a, b, add2(a, b))
c, d := .3, .4
fmt.Printf("%f + %f = %f\n", c, d, add(c, d))
fmt.Printf("%f + %f = %f\n", c, d, add2(c, d))
e, f := "Fizz", "Buzz"
fmt.Printf("%s + %s = %s\n", e, f, add(e, f))
fmt.Printf("%s + %s = %s\n", e, f, add2(e, f))
}
func add[T int | float64 | string](a T, b T) T {
return a + b
}
func add2[T Addable](a T, b T) T {
return a + b
}
type Addable interface {
int | float64 | string
}
//Say hi to provided name.
func sayHi(name string) {
fmt.Printf("Hi %v\n", name)
}
//interface
type Drivable interface {
Drive()
}
type Flyable interface {
Fly()
}
type Plane struct {
Make string
}
func (c *Car) Drive() {
fmt.Printf("[%T] %s is driving.\n", c, c.Make)
}
func (t *Truck) Drive() {
fmt.Printf("[%T] %s is driving.\n", t, t.Make)
}
func (p *Plane) Fly() {
fmt.Printf("[%T] %s is flying.\n", p, p.Make)
}
func (v *Vehicle) Drive() {
fmt.Printf("[%T] %s is driving.\n", v, v.Make)
}
func (v *Vehicle) Fly() {
fmt.Printf("[%T] %s is flying.\n", v, v.Make)
}
func StartDriving(d Drivable) {
d.Drive()
}
func StartFlying(f Flyable) {
f.Fly()
}
func interfaceRunner() {
c := Car{Vehicle{"Audi", "R8"}, 200}
StartDriving(&c)
p := Plane{"Boeing"}
StartFlying(&p)
s := Square{2, 2}
ci := Circle{4}
fmt.Println(GetArea(&s))
fmt.Println(GetArea(&ci))
}
func GetArea(s Shape) float64 {
return s.Area()
}
type Shape interface {
Area() float64
}
type Square struct {
Height float64
Width float64
}
func (s *Square) Area() float64 {
return s.Height * s.Width
}
type Circle struct {
Radius float64
}
func (c *Circle) Area() float64 {
return math.Pi * (c.Radius * c.Radius)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment