Skip to content

Instantly share code, notes, and snippets.

@afreeland
Created April 5, 2018 12:49
Show Gist options
  • Save afreeland/72991ba07d609f4bef91b44581672a22 to your computer and use it in GitHub Desktop.
Save afreeland/72991ba07d609f4bef91b44581672a22 to your computer and use it in GitHub Desktop.
Go: basic looping
package main
import (
"fmt"
)
func main() {
fmt.Println("Hello, playground")
for i:=0; i < 5; i++ {
println(i)
}
// Like while loop
k := 0
for {
k++;
println(k);
if k > 5 {
break
}
}
// Loop through collections arrays, slices, maps
s := []string{"foo", "bar", "buz"}
for idx, v := range s {
println(idx, v)
}
// Loop through map
m := make(map[string]string)
m["first"] = "foo"
m["second"] = "bar"
m["third"] = "buz"
for k, mv := range m {
println(k, mv)
}
// Prints the size of map
println(k)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment