Skip to content

Instantly share code, notes, and snippets.

@topherPedersen
Last active December 23, 2019 23:25
Show Gist options
  • Save topherPedersen/2b73da549493172f59e6a1441cb6864c to your computer and use it in GitHub Desktop.
Save topherPedersen/2b73da549493172f59e6a1441cb6864c to your computer and use it in GitHub Desktop.
Learn Golang in 30 Minutes
// REFERENCE (Learn Golang in 12 Minutes): https://www.youtube.com/watch?v=C8LgvuEBraI
package main
import (
"fmt"
)
func main() {
// functions
fmt.Println("hello, golang!")
// variables
var x int = 4
y := 7 // use walrus tooth operator to infer type
// if statements
if (x == 5) {
fmt.Println("x equals five!")
} else if (x != 5 && y == 7) {
fmt.Println("x does not equal five, but y equals seven...")
} else {
fmt.Println("I dunno man?")
}
// arrays (fixed length)
var a [5]int
a[0] = 100
a[1] = 101
a[2] = 102
a[3] = 103
a[4] = 104
fmt.Println(a[0])
// slices (adjustable length "array")
var b []int
b = append(b, 200)
b = append(b, 201)
b = append(b, 202)
b = append(b, 203)
b = append(b, 204)
fmt.Println(b[0])
// maps (key value pairs, "dictionary")
c := make(map[string]int)
c["foo"] = 300
c["bar"] = 301
c["baz"] = 302
fmt.Println(c["foo"])
// loops
// (no while loops in golang, only for loops!)
for i:= 0; i < 5; i++ {
fmt.Println(i)
}
// if you need something like a while loop,
// this can be accomplished with a for loop
// like so...
j := 0
for j < 1 {
fmt.Println("look at me go!")
fmt.Println("just kidding... lets shut it down!")
j = 1
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment