Skip to content

Instantly share code, notes, and snippets.

@topherPedersen topherPedersen/learngo.go
Last active Dec 23, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.