Go Arrays

Introduction

An array in Go is a fixed-length sequence of elements of the same type. Arrays are useful for storing multiple values in a single variable and can be used to implement more complex data structures like slices and lists. In this chapter, you will learn the basics of arrays in Go, including how to declare, initialize, and manipulate them.

Declaring Arrays

To declare an array in Go, you specify the type of elements it will hold and the number of elements it can contain.

Syntax:

var arrayName [size]elementType

Example

Example:

package main

import "fmt"

func main() {
    var numbers [5]int
    fmt.Println(numbers) // Output: [0 0 0 0 0]
}

In this example, numbers is an array of five integers, and since it has not been initialized, all its elements are set to the zero value of the int type, which is 0.

Initializing Arrays

Arrays can be initialized at the time of declaration.

Example:

package main

import "fmt"

func main() {
    numbers := [5]int{1, 2, 3, 4, 5}
    fmt.Println(numbers) // Output: [1 2 3 4 5]
}

You can also let the compiler infer the length of the array based on the number of initializers provided.

Example:

package main

import "fmt"

func main() {
    numbers := [...]int{1, 2, 3, 4, 5}
    fmt.Println(numbers) // Output: [1 2 3 4 5]
}

Accessing Array Elements

Array elements are accessed using their index, with the first element at index 0.

Example:

package main

import "fmt"

func main() {
    numbers := [5]int{1, 2, 3, 4, 5}
    fmt.Println(numbers[0]) // Output: 1
    fmt.Println(numbers[4]) // Output: 5

    numbers[2] = 10
    fmt.Println(numbers) // Output: [1 2 10 4 5]
}

Array Length

The built-in len function returns the length of an array.

Example:

package main

import "fmt"

func main() {
    numbers := [...]int{1, 2, 3, 4, 5}
    fmt.Println(len(numbers)) // Output: 5
}

Iterating Over Arrays

You can iterate over arrays using a for loop or the for-range loop.

For Loop

Example:

package main

import "fmt"

func main() {
    numbers := [5]int{1, 2, 3, 4, 5}
    for i := 0; i < len(numbers); i++ {
        fmt.Println(numbers[i])
    }
}

For-Range Loop

Example:

package main

import "fmt"

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

Multidimensional Arrays

Go supports multidimensional arrays. A multidimensional array is an array of arrays.

Example:

package main

import "fmt"

func main() {
    matrix := [3][3]int{
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9},
    }
    fmt.Println(matrix)
    // Output:
    // [[1 2 3]
    //  [4 5 6]
    //  [7 8 9]]
}

You can access elements in a multidimensional array using multiple indices.

Example:

package main

import "fmt"

func main() {
    matrix := [3][3]int{
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9},
    }
    fmt.Println(matrix[1][1]) // Output: 5

    matrix[1][1] = 10
    fmt.Println(matrix)
    // Output:
    // [[1 2 3]
    //  [4 10 6]
    //  [7 8 9]]
}

Conclusion

Arrays in Go are fixed-length sequences of elements of the same type. They are useful for storing and managing collections of data. By understanding how to declare, initialize, access, and manipulate arrays, you can effectively use them in your Go programs. Additionally, you can work with multidimensional arrays to handle more complex data structures. Arrays are a fundamental building block for more advanced data structures like slices and lists in Go.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top