EZ

Eduzan

Learning Hub

Eduzan
Eduzan / Go Programming Language

Arrays in Go

Understanding Arrays in Go Programming Language

Arrays in Go (or Golang) are conceptually similar to arrays in other programming languages. They are particularly useful when you need to manage a collection of data of the same type, such as storing a list of student scores. An array is a fixed-length data structure used to store homogeneous elements in memory. However, because arrays have a fixed size, slices are often preferred in Go.

Key Features of Arrays in Go

  • Arrays are fixed in size, meaning their length cannot change once defined.
  • Arrays allow zero or more elements of the same type.
  • Array elements are accessed using their zero-based index, with the first element at index array[0] and the last at array[len(array)-1].
  • Arrays are mutable, allowing modification of elements by their index.

Creating and Accessing Arrays in Go

Using the var Keyword

Arrays in Go can be declared using the var keyword.

Syntax:

var array_name [length]Type

Key Points

  • Arrays are mutable, so you can update their elements using the index
var array_name [length]Type
array_name[index] = value
  • Access elements using their index or iterate through the array using a loop.
  • Arrays in Go are one-dimensional by default.
  • Duplicate elements are allowed.
package main

import "fmt"

func main() {
    var numbers [5]int
    numbers[0] = 10
    numbers[1] = 20
    numbers[2] = 30
    numbers[3] = 40
    numbers[4] = 50

    fmt.Println("Array elements:")
    for i := 0; i < len(numbers); i++ {
        fmt.Println(numbers[i])
    }
}

Output:

Array elements:
10
20
30
40
50

Using Shorthand Declaration

A shorthand declaration allows you to define and initialize an array in a single line.

Syntax:

array_name := [length]Type{element1, element2, ..., elementN}

Example 2: Shorthand Declaration

package main

import "fmt"

func main() {
    colors := [3]string{"Red", "Green", "Blue"}

    fmt.Println("Colors in the array:")
    for _, color := range colors {
        fmt.Println(color)
    }
}

Output:

Array elements:
10
20
30
40
50

Using Shorthand Declaration: A shorthand declaration allows you to define and initialize an array in a single line.

Syntax:

array_name := [length]Type{element1, element2, ..., elementN}

Example 2: Shorthand Declaration

package main

import "fmt"

func main() {
    colors := [3]string{"Red", "Green", "Blue"}

    fmt.Println("Colors in the array:")
    for _, color := range colors {
        fmt.Println(color)
    }
}

Output:

Colors in the array:
Red
Green
Blue

Multi-Dimensional Arrays

In Go, arrays are one-dimensional, but you can create multi-dimensional arrays (arrays of arrays). These arrays allow you to store data in tabular or matrix format.

Syntax

var array_name [Length1][Length2]...[LengthN]Type

Example:

package main

import "fmt"

func main() {
    matrix := [2][2]int{{1, 2}, {3, 4}}

    fmt.Println("Matrix elements:")
    for i := 0; i < 2; i++ {
        for j := 0; j < 2; j++ {
            fmt.Printf("%d ", matrix[i][j])
        }
        fmt.Println()
    }
}

Output:

Matrix elements:
1 2
3 4
End of lesson.