Arrays in Golang

Tram Ho

1. Arrays and basic principles

Today I will talk about arrays (arrays) in Go . Arrays are an important data structure in most programming languages. In Go is no exception. Structures like slices and maps in Go are also built with arrays. Understanding how arrays work will help you realize the power of slices and maps in Go .

1.1 Concepts

An array in Go is a fixed-length data type containing a block of data of the same type. It can be primitive types like int and string or it can be of type struct.

You can see the example below that illustrates array. The elements of an array are placed inside a gray box and placed in succession. Each element has the same type, in the illustration it is an integer , and it can be accessed via a single location.

An array is a valuable data structure. You can access each element of the array quickly using the index. The array type provides the amount of space in memory that you must move to find each element. Since each element is of the same type and is concatenated, moving between elements in the array is consistent and fast.

1.2 Declaring and initializing

An array is declared by specifying the type of data to be stored and the total number of words (also known as the length of the array).

When an array is declared, the data type and the length of the array cannot be changed anymore. If you need to add elements, you need to create a new array of the desired length, then copy the value from the old array to the new array.

When a variable in Go is declared, they are always assigned a default initialization value, and the array is no exception. When an array is initialized, each element is initialized to zero. As you can see below, each element in an array of type will be initialized to 0.

A quick and simple way to declare and initialize an array is to use an array literal (I don’t know how to translate it yet). Array literals allow you to declare the number of elements and the value of each element in an array

If the length of the array received , Go determines the length of the array based on the number of elements initialized.

1.3 Working with arrays

As you already know, arrays are an efficient data structure because memory is organized in sequence. This gives the advantage of accessing each element of the array. To access each element of the array using the [] operator

You can also use pointers with arrays. Use the star (*) operator to access the value that each pointer points to. The illustration below:

In Go , array is a value. This means you can use it with the assignment operation operator. Variable names represent the entire array, so you can assign arrays of the same type.

After performing the assignment operator, you will have 2 identical arrays. As shown below:

The type of an array variable includes both the length and the type of data it can store for each element. You can only use the assignment operator with arrays of the same type

Copying a pointer array copies the value of the pointer, not the value that the pointer is pointing to.

After copying, you have 2 arrays pointing to the same string. As shown below:

1.4 Passing arrays between functions (Passing arrays between functions)

Transferring arrays between functions can affect performance and be memory expensive. When you pass a variable between functions, they always pass a value. When your variable is an array, regardless of its size, it creates a copy and passes it to the function. See the illustration below:

Every time the foo function is called 8 megabytes will be allocated on the stack, then they are copied and passed to the foo function. Go can handle this replication, but to be more efficient you should pass a pointer and it only copies 8 bytes, instead of 8 megabytes of the memory allocated on the stack.

When the function foo is called, it gets a pointer to an array of 1 million integer elements. The function will now take the address of the array, which requires only 8 bytes of memory to be allocated on the stack for the pointer variable. This action will help optimize memory performance. You only need to be aware of this because when you use a pointer, changing the value that the pointer is pointing to changes the value on the shared memory.

The article on arrays is quite long, but I think this content also provides quite a full range of declaration, usage, as well as the optimal way to use arrays passed between functions. If you find it interesting, please give me a like so that I will be motivated to do more of the following articles

Link to the original post here, guys .

Share the news now

Source : Viblo