Created
April 5, 2018 12:49
-
-
Save afreeland/72991ba07d609f4bef91b44581672a22 to your computer and use it in GitHub Desktop.
Go: basic looping
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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