How to Iterate Over a Slice in Golang?
You can loop through the list items by using a for loop.
Example
package main
import "fmt"
func main() {
var strSlice = []string{"India", "Canada", "Japan", "Germany", "Italy"}
fmt.Println("\n---------------Example 1 --------------------\n")
for index, element := range strSlice {
fmt.Println(index, "--", element)
}
fmt.Println("\n---------------Example 2 --------------------\n")
for _, value := range strSlice {
fmt.Println(value)
}
j := 0
fmt.Println("\n---------------Example 3 --------------------\n")
for range strSlice {
fmt.Println(strSlice[j])
j++
}
}
Most Helpful This Week
Multiple-value <function> in single-value context error in Golang
Example: ReadAll, ReadDir, and ReadFile from IO Package
Expected <type>, but got <type> error in Golang
Different ways for Integer to String Conversions
React JS Count number of checkboxes are checked
Make Your Retirement Luxurious with These 5 Game-Changing Altcoins
How do you create an HTTP client in Go?
Program in Go language to Program to Add Two Matrix Using Multi-dimensional Arrays
What is JSX?
What is an HTTP client in Go?