Answers for "golang for loop range"

Go
1

for range go

arr = [1,2,3,4]
for i, val := range arr {
	print(i, val) // prints iterator, value
}
Posted by: Guest on May-05-2021
1

golang iterate through slice

var slice = []string{"Apple", "Orange", "Kiwi"}
for index, sliceItem := range slice { // You can omit index or sliceItem with a _
  fmt.Println("Index in slice:", index)
  fmt.Println("Fruit:", sliceItem)
}
Posted by: Guest on June-12-2020
4

golang loop

package main

import "fmt"

func main() {
	sum := 0
	for i := 0; i < 10; i++ {
		sum += i
	}
	fmt.Println(sum)
}
Posted by: Guest on March-26-2020
1

go iterate over slice

nums := []int{2, 3, 4}
    sum := 0
    for _, num := range nums {
        sum += num
    }
    fmt.Println("sum:", sum)
Posted by: Guest on July-30-2020

Browse Popular Code Answers by Language