for range go code example
Example 1: go iterate over slice
nums := []int{2, 3, 4}
sum := 0
for _, num := range nums {
sum += num
}
fmt.Println("sum:", sum)
Example 2: range in go
kvs := map[string]string{"a": "apple", "b": "banana"}
for k, v := range kvs {
fmt.Printf("%s -> %s\n", k, v)
}
Example 3: golang range arrat
for i, num := range nums {
if num == 3 {
fmt.Println("index:", i)
}
}
Example 4: for range go
arr = [1,2,3,4]
for i, val := range arr {
print(i, val) // prints iterator, value
}