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
+6.2k Golang : Totalize or add-up an array or slice example
+33.4k Golang : Create x509 certificate, private and public keys
+9.3k Golang : interface - when and where to use examples
+18.8k Golang : Execute shell command
+37.3k Golang : Comparing date or timestamp
+4.4k Unix/Linux : How to pipe/save output of a command to file?
+9.2k Golang : Read file with ioutil
+7.8k Golang : Routes multiplexer routing example with regular expression control
+35.2k Golang : Save image to PNG, JPEG or GIF format.
+11.7k Golang : Simple client-server HMAC authentication without SSL example
+21.4k Golang : How to reverse slice or array elements order
+11k Golang : Format numbers to nearest thousands such as kilos millions billions and trillions