Exploring Array Iteration in Go

Exploring Array Iteration in Go

Explore the differences and similarities between Go and the popular programming languages when it comes to iterating over arrays

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.

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

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

for i := 0; i < len(arr); i++ {
    fmt.Printf("Index: %d, Value: %d\n", i, arr[i])
}
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]);
}
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.

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

for index, value := range arr {
    fmt.Printf("Index: %d, Value: %d\n", index, value)
}
arr = [1, 2, 3, 4, 5]

for index, value in enumerate(arr):
    print(f"Index: {index}, Value: {value}")
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.

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

i := 0
for i < len(arr) {
    fmt.Printf("Index: %d, Value: %d\n", i, arr[i])
    i++
}
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++;
}
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

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

for index := range arr {
    fmt.Printf("Index: %d, Value: %d\n", index, arr[index])
}
val array = arrayOf(1, 2, 3, 4, 5)

for (i in array.indices) {
    println("Index: $i, Value: ${array[i]}")
}
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.