Golang : How to iterate over a []string(array)
Sometimes the easiest thing to do is the hardest thing to remember... like how to iterate through a string array. :P
This is a simple for loop tutorial showing how to iterate over the values inside []string or any kind of arrays.
package main
import (
"fmt"
)
func main() {
// taken from http://www.asitis.com/16/
divineValues := []string{
"fearlessness",
"purification of one's existence",
"cultivation of spritual knowledge",
"charity",
"self-control",
"performance of sacrifice",
"study of the Vedas",
"austerity and simplicity",
"non-violence",
"truthfulness",
"freedom from anger",
"renunciation",
"tranquility",
"aversion to faultfinding",
"compassion and freedom from covetousness",
"gentleness",
"modesty and steady determination",
"vigor",
"forgiveness",
"fortitude",
"cleanliness",
"freedom from envy and the passion for honor",
}
for index, each := range divineValues {
fmt.Printf("Divine value [%d] is [%s]\n", index, each)
}
}
See also : Golang : Iterating Elements Over A List
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+20.2k Android Studio : AlertDialog and EditText to get user string input example
+16.7k Golang : Get input from keyboard
+5.9k Golang : Extract XML attribute data with attr field tag example
+25.5k Golang : How to read integer value from standard input ?
+6.8k Restart Apache or Nginx web server without password prompt
+8.1k Golang : Convert word to its plural form example
+6.1k Golang : Test input string for unicode example
+11.3k Golang : Find age or leap age from date of birth example
+18.4k Golang : Implement getters and setters
+32k Golang : Convert []string to []byte examples
+7k CloudFlare : Another way to get visitor's real IP address
+8.8k Golang : How to capture return values from goroutines?