Array in Golang

In Go, an array is a fixed-size, ordered collection of elements of the same data type. Arrays provide a way to store multiple values under a single variable name. The size of an array is determined at the time of declaration and cannot be changed during runtime.

Imagine an array as a series of storage slots, each labeled with an index number. Each slot can hold a single value of the specified data type. Here’s an example of an array of integers:

Index:    0    1    2    3    4
Value:   [10] [20] [30] [40] [50]

In this illustration, we have an array of integers with index numbers from 0 to 4. Each index corresponds to a specific slot that holds a value. For instance, index 0 holds the value 10, index 1 holds 20, and so on.

Keep in mind that arrays have a fixed size, so the number of slots is determined at the time of declaration and cannot be changed. If you need more flexibility, you might consider using slices, which are a more dynamic data type in Go.

Here’s how you can define and use arrays in Go:

1. Defining an Array: To define an array, specify the data type of its elements and the number of elements enclosed in square brackets [].

// Defining an array of integers with 5 elements
var numbers [5]int

2. Initializing an Array: You can initialize an array during declaration by providing the values enclosed in curly braces {}.

// Initializing an array with values
var weekdays [7]string = [7]string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}

3. Accessing Elements: You can access elements of an array using zero-based indexing.

// Accessing elements of an array
fmt.Println("First day:", weekdays[0])
fmt.Println("Second day:", weekdays[1])

4. Array Length: The length of an array is fixed and determined at declaration.

// Getting the length of an array
length := len(weekdays)
fmt.Println("Number of days:", length)

5. Iterating Over an Array: You can use loops like for to iterate over the elements of an array.

// Iterating over an array
for i := 0; i < len(numbers); i++ {
    fmt.Println("Element", i, ":", numbers[i])

6. Array Initialization with Short Syntax: You can also use a short syntax to initialize arrays without explicitly specifying the length.

Arrays are useful when you know the exact number of elements you need and you don’t need to change the size dynamically. However, slices (a more flexible data type) are commonly used in Go for dynamic collections of elements.

Example :

package main

import "fmt"

func main() {
    // Defining and initializing an array of integers
    var numbers [5]int = [5]int{10, 20, 30, 40, 50}

    // Accessing elements of the array
    fmt.Println("Element at index 0:", numbers[0])
    fmt.Println("Element at index 2:", numbers[2])

    // Modifying an element
    numbers[3] = 45
    fmt.Println("Modified element at index 3:", numbers[3])

    // Iterating over the array
    fmt.Println("Array elements:")
    for i := 0; i < len(numbers); i++ {
        fmt.Printf("Element at index %d: %d\n", i, numbers[i])

Output :

Element at index 0: 10
Element at index 2: 30
Modified element at index 3: 45
Array elements:
Element at index 0: 10
Element at index 1: 20
Element at index 2: 30
Element at index 3: 45
Element at index 4: 50

In this example, we define an array numbers with 5 integer elements. We access and modify specific elements using their index numbers. Then, we iterate over the array using a for loop and print each element along with its index. This illustrates how arrays work in Go and how you can interact with them.