Use [] to find tag! Example: [flutter, javascript]

Learn Go Programming Language
Learn Go Programming Language

Chapter 5.7

Array Data Type

remove_red_eye 1688 Times
spellcheck 445 Words, 2744 Characters
*Note: this book is still ongoing until it has a finished label.

The Go programming language, often referred to as Golang, is known for its simple syntax and high performance. One of the fundamental features in Golang is the array data type. Although it may seem basic, arrays in Golang offer an effective way to store and access a fixed-size collection of elements.

What is an Array?

An array is a collection of elements of the same data type, stored in contiguous memory locations. In Golang, arrays have a fixed size that is specified when they are declared. The size of an array cannot be changed once it is defined.

Declaring and Initializing Arrays

To declare an array in Golang, you need to specify the size and the type of elements it will store. Here is an example of declaring an array:

var numbers [5]int

The above array numbers has five elements, all of type int. You can also initialize the array at the time of declaration:

var primes = [5]int{2, 3, 5, 7, 11}

If you want Golang to infer the size of the array, you can use `...`:

var languages = [...]string{"Go", "Python", "Java", "C++"}


Accessing and Modifying Array Elements

You can access elements of an array using their index, which starts from 0. To access the first element of the primes array:

firstPrime := primes[0]

To modify the third element of the numbers array:

numbers[2] = 10

Iterating Through Arrays

You can iterate through an array using a for loop. Here is an example of iterating through the primes array:

firstPrime := primes[0]

You can also use a more idiomatic for-range loop:

for index, value := range primes {
    fmt.Printf("Index: %d, Value: %d\n", index, value)
}

Multidimensional Arrays

Golang also supports multidimensional arrays, which are arrays of arrays. Here is an example of declaring a two-dimensional array:

var matrix [3][3]int

You can initialize a two-dimensional array like this:

matrix := [3][3]int{
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9},
}

Accessing elements of a two-dimensional array is done like this:

element := matrix[1][2] // Accesses the element in the second row, third column

Advantages and Limitations of Arrays

Arrays in Golang are highly efficient and simple to use when the size of the data is known and fixed. However, if you need a dynamic or flexible data structure, it might be better to use slices, which are an abstraction over arrays that allow for dynamic sizing.

Conclusion

Arrays in Golang are a powerful and efficient data structure that allows for storing elements of the same type in a fixed order. By understanding how to declare, initialize, access, and modify arrays, you can leverage the power of arrays in Go application development. Although they have limitations in terms of fixed size, arrays remain an excellent choice for many use cases requiring performance and reliability.

Next Article (Slice Data Type)
Content Navigation