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
+33.9k Golang : How to split or chunking a file to smaller pieces?
+11.9k CodeIgniter : "Fatal error: Cannot use object of type stdClass as array" message
+10k Golang : How to use if, eq and print properly in html template
+10.4k Golang : Convert decimal number(integer) to IPv4 address
+18k Golang : Convert seconds to human readable time format example
+8.2k Golang : How to generate Code 39 barcode?
+13.5k Golang : Search folders for file recursively with wildcard support
+13.6k nginx: [emerg] unknown directive "ssl"
+4.4k PHP : Hide PHP version information from curl
+19.5k Golang : Create and resolve(read) symbolic links
+4.6k PHP : Convert CSV to JSON with YQL example