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
+14.2k Golang : Overwrite previous output with count down timer
+21.8k Golang : How to run Golang application such as web server in the background or as daemon?
+5k Golang : Calculate half life decay example
+9.2k Golang : Get all countries currencies code in JSON format
+4.9k Golang : Customize scanner.Scanner to treat dash as part of identifier
+8.3k Golang : Convert(cast) []byte to io.Reader type
+7k Golang : Example of custom handler for Gorilla's Path usage.
+6.9k Golang : Transform lisp or spinal case to Pascal case example
+22.3k Golang : Set and Get HTTP request headers example
+4.6k Which content-type(MIME type) to use for JSON data
+4.5k MariaDB/MySQL : Form select statement or search query with Chinese characters
+13.1k Golang : Generate Code128 barcode