Go Program to Print Array Items in Even Index Position

Write a Go program to Print the Array items in an even index position using For loop. In this Go example, the for loop (for i := 0; i < len(numarray); i += 2) starts iteration at 0 and increments by 2. Within the loop, we print all the array items at an even position.

package main

import "fmt"

func main() {

    numarray := []int{10, 20, 30, 40, 50, 60, 70, 80}

    fmt.Println("The List of Array Items in Even Index Position = ")
    for i := 0; i < len(numarray); i += 2 {
        fmt.Println(numarray[i])
    }

} 
The List of Array Items in Even Index Position = 
10
30
50
70

Golang Program to Print Array Items in Even Index Position using the For Loop range

We used an extra if statement (if i%2 == 0) to check whether the index position divisible by two equals two, which means it is an even index position. Next, print that even position array number.

package main

import "fmt"

func main() {

    numarray := []int{10, 20, 30, 40, 50, 60, 70, 80}

    fmt.Println("The List of Array Items in Even index Position = ")
    for i, _ := range numarray {
        if i%2 == 0 {
            fmt.Println(numarray[i])
        }
    }
}
The List of Array Items in Even index Position = 
10
30
50
70

This Golang program allows entering the array size, items, and pints the elements at an even index position.

package main

import "fmt"

func main() {

    var size int

    fmt.Print("Enter the Even Odd Array Size = ")
    fmt.Scan(&size)

    numarray := make([]int, size)

    fmt.Print("Enter the Even Odd Array Items  = ")
    for i := 0; i < size; i++ {
        fmt.Scan(&numarray[i])
    }

    fmt.Println("The List of Array Items in Even Index Position = ")
    for i := 0; i < len(numarray); i += 2 {
        fmt.Println(numarray[i])
    }
}
Go Program to Print Array Items in Even Index Position 3