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
+7.2k Android Studio : Import third-party library or package into Gradle Scripts
+16k Golang : delete and modify XML file content
+3.9k Golang : Print instead of building pyramids
+6.2k Golang : Word limiter example
+5.6k Unix/Linux : How to fix CentOS yum duplicate glibc or device-mapper-libs dependency error?
+3.1k Detect if Google Analytics and Developer Media are loaded properly or not
+5k PHP : How to check if an array is empty ?
+25k Golang : Calculate future date with time.Add() function
+11.9k Golang : Count number of runes in string
+33.4k Golang : Upload and download file to/from AWS S3
+6.4k Golang : Handling Yes No Quit query input
+9.6k Golang : Command line file upload program to server example