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
+5.9k Linux : Disable and enable IPv4 forwarding
+52.8k Golang : How to get struct field and value by name
+11k Golang : Removes punctuation or defined delimiter from the user's input
+27.8k PHP : Convert(cast) string to bigInt
+11.3k Google Maps URL parameters configuration
+13.8k Android Studio : Password input and reveal password example
+7.2k Golang : A simple forex opportunities scanner
+6.1k Golang : Convert Chinese UTF8 characters to Pin Yin
+7.9k Golang : Load DSA public key from file example
+9.6k Golang : Qt Yes No and Quit message box example
+7.1k Golang : How to solve "too many .rsrc sections" error?
+14.7k Android Studio : Use image as AlertDialog title with custom layout example