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
Converting Int data type to Float in Go
How do you handle HTTP Client server load balancing in Go?
10 Countries Paying Highest Salaries to Cybersecurity Professionals
Syntax error: unexpected <token> error in Golang
Web Application to generate QR code in Golang
How do you handle HTTP server caching in Go?