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
+10k Golang : Detect number of active displays and the display's resolution
+10.8k Golang : Allow Cross-Origin Resource Sharing request
+12.3k Golang : Pagination with go-paginator configuration example
+10.7k Golang : Generate 403 Forbidden to protect a page or prevent indexing by search engine
+6.2k Fontello : How to load and use fonts?
+9.2k Golang : Build and compile multiple source files
+30.3k Golang : Get time.Duration in year, month, week or day
+7.7k Golang : How to stop user from directly running an executable file?
+10.4k Golang : Wait and sync.WaitGroup example
+12.3k Golang : Detect user location with HTML5 geo-location
+22.4k Golang : How to run Golang application such as web server in the background or as daemon?
+5.9k Golang : Markov chains to predict probability of next state example