# Exploring Array Iteration in Go

## **Introduction**

I've worked with various programming languages throughout my career. Recently, I started working with Go, a language that has been gaining popularity in the development community.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1676840981099/9e5f4d82-1606-4836-b9cf-8dd7720fb7be.png?auto=compress,format&format=webp align="center")

I have been working with other programming languages such as C, C++, Kotlin, and Python, and I was curious to see how Go compares.  
In this blog post, I'll be exploring the similarities and differences between Go and some of the other languages I have experience with, specifically in the context of iterating over arrays.

For all the cases the code will initialize an array with values and then iterate over the array using one of the methods. During each iteration, the code prints the current index and value of the array.

## **A for loop with an index**

This method is commonly used in C language to iterate over an array using a for loop and index, here is how it looks like in both Go & C

```go
arr := []int{1, 2, 3, 4, 5}

for i := 0; i < len(arr); i++ {
    fmt.Printf("Index: %d, Value: %d\n", i, arr[i])
}
```

```c
int arr[5] = {1, 2, 3, 4, 5};

int i;
for (i = 0; i < sizeof(arr) / sizeof(arr[0]); i++) {
    printf("Index: %d, Value: %d\n", i, arr[i]);
}
```

```plaintext
Output: Index: 0, Value: 1
        Index: 1, Value: 2
        Index: 2, Value: 3
        Index: 3, Value: 4
        Index: 4, Value: 5
```

## **A for range loop with an index**

This method is commonly used in Python language, which iterates over the elements of a list or array, you can see here how Go & Python does it.

```go
arr := []int{1, 2, 3, 4, 5}

for index, value := range arr {
    fmt.Printf("Index: %d, Value: %d\n", index, value)
}
```

```python
arr = [1, 2, 3, 4, 5]

for index, value in enumerate(arr):
    print(f"Index: {index}, Value: {value}")
```

```plaintext
Output: Index: 0, Value: 1
        Index: 1, Value: 2
        Index: 2, Value: 3
        Index: 3, Value: 4
        Index: 4, Value: 5
```

## **A while loop with an index**

This method is commonly used in C language to iterate over an array using a while loop and index, here is how it looks like in Go and in C.

```go
arr := []int{1, 2, 3, 4, 5}

i := 0
for i < len(arr) {
    fmt.Printf("Index: %d, Value: %d\n", i, arr[i])
    i++
}
```

```c
int arr[5] = {1, 2, 3, 4, 5};

int i = 0;
while (i < sizeof(arr) / sizeof(arr[0])) {
    printf("Index: %d, Value: %d\n", i, arr[i]);
    i++;
}
```

```plaintext
Output: Index: 0, Value: 1
        Index: 1, Value: 2
        Index: 2, Value: 3
        Index: 3, Value: 4
        Index: 4, Value: 5
```

## **A for loop with range over array's indexes**

This method returns an iterable object of integers that can be used to iterate over indexes, which can be done in a very similar way also in Kotlin

```go
arr := []int{1, 2, 3, 4, 5}

for index := range arr {
    fmt.Printf("Index: %d, Value: %d\n", index, arr[index])
}
```

```kotlin
val array = arrayOf(1, 2, 3, 4, 5)

for (i in array.indices) {
    println("Index: $i, Value: ${array[i]}")
}
```

```plaintext
Output: Index: 0, Value: 1
        Index: 1, Value: 2
        Index: 2, Value: 3
        Index: 3, Value: 4
        Index: 4, Value: 5
```

## **Conclusion**

Go brings the best of both worlds by providing a language with a simple syntax and ease of writing while also maintaining high performance.  
Whether you're looking to write a small script or build a complex system, Go has the capabilities to fit your needs. With its growing popularity and active community, Go is a language to consider adding to your programming toolset.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1676921783106/2ae71a7f-17b4-4eb0-99e7-a2f8e95d1d24.png align="center")
