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