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
+5.6k Golang : Find change in a combination of coins example
+6.6k Golang : Find the longest line of text example
+13.1k Golang : Generate Code128 barcode
+10.1k Golang : Simple Jawi(Yawi) to Rumi(Latin/Romanize) converter
+5.3k Golang : Display advertisement images or strings on random order
+20.2k Golang : Pipe output from one os.Exec(shell command) to another command
+48.3k Golang : Upload file from web browser to server
+5.6k Golang : Fix opencv.LoadHaarClassifierCascade The node does not represent a user object error
+22.5k Golang : Round float to precision example
+3.1k Golang : Fix go-cron set time not working issue
+17.5k Golang : Read data from config file and assign to variables
+9.2k Golang : Scramble and unscramble text message by randomly replacing words