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
+13.9k Golang : convert(cast) string to float value
+11.2k Google Maps URL parameters configuration
+14.6k Golang : Convert(cast) int to float example
+23.9k Golang : Fix type interface{} has no field or no methods and type assertions example
+7.9k Golang : Trim everything onward after a word
+6.7k Golang : Check if password length meet the requirement
+31.7k Golang : How to convert(cast) string to IP address?
+8.5k Golang : Generate Datamatrix barcode
+6k Fontello : How to load and use fonts?
+10k Golang : Read file and convert content to string
+13.7k Golang : Qt progress dialog example
+7.2k Golang : Use modern ciphers only in secure connection