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
+6k Golang : How to iterate a slice without using for loop?
+29.2k Golang : How to redirect to new page with net/http?
+8.9k Golang : Channels and buffered channels examples
+32.2k Golang : Create x509 certificate, private and public keys
+7.1k Prevent Write failed: Broken pipe problem during ssh session with screen command
+14.1k Golang : Validate hostname
+12.5k Golang : Human readable time elapsed format such as 5 days ago
+23.7k Golang : Storing cookies in http.CookieJar example
+4.8k Golang : Error handling methods
+42.6k Golang : Use wildcard patterns with filepath.Glob() example
+14k Golang : Get digits from integer before and after given position example
+16.1k Golang : Find smallest number in array