Skip to content

Instantly share code, notes, and snippets.

@cjgiridhar
Created January 26, 2019 01:15
Show Gist options
  • Save cjgiridhar/52dde2f56d4feb561f43e2f766bf02fb to your computer and use it in GitHub Desktop.
Save cjgiridhar/52dde2f56d4feb561f43e2f766bf02fb to your computer and use it in GitHub Desktop.
Iterating over arrays in Golang
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