Golang Tutorial Index


The concept of arrays in the Go programming language is one of its key features that is parallel to other programming languages. In this tutorial, we will dive deep into arrays in Go and learn how to use them in your code effectively.



What Is an Array in Golang?

In Go, an array is a fixed-size collection of elements of the same type. Each element of an array can be accessed by its index, which starts from 0. The size of an array is defined at the time of its creation and cannot be changed afterward. This means that once an array is created, it cannot be resized.

In Golang programs, programmers often need to store collections of similar data types, such as a list of patients in a hospital, a list of students appearing for an exam, etc. This same type of information must be stored under the same variable name. Thus, programmers can keep them in the program using an array.

Declaring Arrays in Go

Go arrays are declared using square brackets [] and the type of the value they store, as in the following syntax:

Syntax:

var arrayName [arraySize]dataType

For example, the following code declares an array of integers with ten elements:

Example:

var numbers [10]int

The code above declares an array called numbers with ten elements of type int. Once declared, the size of a Go array cannot be changed. This means that arrays in Go are immutable and cannot be resized.

Initialize Arrays in Go

Go arrays can be initialized in various ways, depending on the developer's needs. The most common method is to specify its values within curly braces {} when it is declared.

Example:

var numbers = [10]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

In the above example, an array of 10 int values is declared and initialized.

Another way to initialize Go arrays is to use a for loop to iterate through the elements of the array and assign values.

Example:

package main

func main() {
    var numbers [10]int
    for i := 0; i < len(numbers); i++ {
        numbers[i] = i
    }
}

In the above example, a for loop is used to assign values to the elements of the array numbers.

Accessing Array Elements

Array elements can be accessed using their index. For example, you would use numbers[0] to access the first element of the numbers array. Similarly, you would use number[1] and so on to access the second element.

Example:

package main
import "fmt"

func main() {
    var numbers = [10]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
    fmt.Println(numbers[1]) // outputs 1
}

Looping Over Arrays

Arrays in Go can be easily looped over using the for loop. For example, the following code prints all elements of the numbers array:

Example:

package main
import "fmt"

func main() {
    var numbers = [10]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
    for i, number := range numbers {
        fmt.Printf("Index: %d, Number: %d\n", i, number)
    }
}

Multidimensional Arrays in Go

One-dimensional arrays are majorly used in the Go programming language. In addition, Golang also supports multidimensional arrays, which are arrays of arrays. Multidimensional arrays can be declared using the following syntax:

Syntax:

var arrayName [rowSize][columnSize]dataType

For example, the following code declares a 2-dimensional array of integers with three rows and four columns:

Example:

var matrix [3][4]int

Conclusion

In this tutorial, we have covered the basics of arrays in Go, including how to declare, access, and loop over arrays. We have also discussed multidimensional arrays. With this understanding, you should now be able to use arrays in your Go code effectively.



Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram