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
+11.2k CodeIgniter : How to check if a session exist in PHP?
+15.7k Golang : Get checkbox or extract multipart form data value example
+7.9k Javascript : Put image into Chrome browser's console
+6.4k Golang : How to search a list of records or data structures
+14.2k Golang : syscall.Socket example
+17.3k Golang : How to tell if a file is compressed either gzip or zip ?
+15.9k Golang : Update database with GORM example
+5.3k Javascript : Shuffle or randomize array example
+4.9k Unix/Linux : secure copying between servers with SCP command examples
+11.5k Golang : Change date format to yyyy-mm-dd
+14.6k Golang : Execute function at intervals or after some delay
+7.9k Golang : Ways to recover memory during run time.