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
+17.3k Convert JSON to CSV in Golang
+10.3k Golang : Bubble sort example
+36.1k Golang : Convert date or time stamp from string to time.Time type
+4.4k Golang : How to pass data between controllers with JSON Web Token
+12k Golang : Flush and close file created by os.Create and bufio.NewWriter example
+51.6k Golang : How to get time in milliseconds?
+9.1k Golang : How to protect your source code from client, hosting company or hacker?
+5.2k Golang : Get S3 or CloudFront object or file information
+18.1k Golang : Get download file size
+11.3k Golang : Simple file scaning and remove virus example
+11.7k Golang : Convert a rune to unicode style string \u
+10.2k Generate Random number with math/rand in Go