Created
January 26, 2019 01:15
-
-
Save cjgiridhar/52dde2f56d4feb561f43e2f766bf02fb to your computer and use it in GitHub Desktop.
Iterating over arrays in Golang
This file contains 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() { | |
/* Short hand declaration */ | |
mynames := [3]string{"Alice", "Bob", "Celine"} | |
/* Iterate through array using indices */ | |
for i := 0; i < len(mynames); i++ { | |
fmt.Printf("%s", mynames[i]) | |
} | |
/* Using range function */ | |
for i, v := range mynames { | |
fmt.Println(i, v) | |
} | |
/* Ignoring the index with _ */ | |
for _, v := range mynames { | |
fmt.Println(v) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment