Skip to content

Instantly share code, notes, and snippets.

@shockalotti
shockalotti / Go Golang - If statement
Last active August 29, 2015 14:01
Go Golang - If statement
package main
import "fmt"
func main() {
for i:=1; i<=50; i++ {
if i % 2 == 0 {
fmt.Println("even")
} else if i % 3 == 0 {
fmt.Println("odd")
@shockalotti
shockalotti / Go Golang - basic switch statement
Created May 26, 2014 05:15
Go Golang - basic switch statement
package main
import "fmt"
func main() {
for i:=0; i<=5; i++ {
switch i {
case 0: fmt.Println("Zero")
case 1: fmt.Println("One")
case 2: fmt.Println("Two")
@shockalotti
shockalotti / Go Golang - basic map usage
Created May 27, 2014 04:11
Go Golang - basic map usage
package main
import "fmt"
func main(){
x:=make(map[string]int)
x["key"] = 10
fmt.Println(x["key"])
}
@shockalotti
shockalotti / Go Golang - basic map function #2
Last active August 29, 2015 14:01
Go Golang - basic map function #2
package main
import "fmt"
func main() {
elements:= make(map[string]string)
elements["H"] = "Hydrogen"
elements["He"] = "Helium"
elements["Li"] = "Lithium"
elements["Be"] = "Beryllium"
@shockalotti
shockalotti / Go Golang - shorter way of writing map
Created May 27, 2014 04:42
Go Golang - shorter way of writing map
package main
import "fmt"
func main() {
elements:= map[string]string {
// shorter way of listing key pairs
"H":"Hydrogen",
"He":"Helium",
"Li":"Lithium",
@shockalotti
shockalotti / Go Golang - multi column map
Created May 27, 2014 04:51
Go Golang - multi column map
package main
import "fmt"
func main() {
// a map of other maps
elements:= map[string]map[string]string {
"H":map[string]string {
"name":"Hydrogen",
@shockalotti
shockalotti / Go Golang - find highest and lowest number
Created May 27, 2014 05:38
Go Golang - find highest and lowest number
package main
import "fmt"
func main() {
x:= []int {
48,96,86,68,
57,82,63,70,
37,34,83,27,
19,97,9,17,
@shockalotti
shockalotti / Go Golang - average function
Created May 27, 2014 07:26
Go Golang - average function
package main
import "fmt"
func average(xs[]float64)float64 {
total:=0.0
for _,v:=range xs {
total +=v
}
return total/float64(len(xs))
@shockalotti
shockalotti / Go Golang - basic passing argument to function
Created May 27, 2014 07:37
Go Golang - basic passing argument to function
package main
import "fmt"
func f(x int) {
fmt.Println(x)
}
func main() {
x:=5
@shockalotti
shockalotti / Go Golang - basic function call stack
Created May 27, 2014 07:40
Go Golang - basic function call stack
package main
import "fmt"
func f2() int {
return 1
}
func f1() int {
return f2()
}