What does golang array store?

(*-*)浩
Release: 2019-12-31 10:35:58
Original
1914 people have browsed it

What does golang array store?

Array refers to a series of collections of the same type of data. Each data contained in the array is called an array element. This type can be any primitive type, such as int, string, etc., or a user-defined type.(Recommended Learning:Go)

A number of elements contained in an array is called the length of the array. In Golang, array is a fixed-length data type, and the length of the array is part of the type, which means [5]int and [10]int are two different types.

Another feature of arrays in Golang is the continuity of occupied memory, which means that the elements in the array are allocated to consecutive memory addresses, so indexing array elements is very fast.

Characteristics of Golang arrays

We can summarize the characteristics of Golang arrays into the following three points:

Fixed length:This means that the array cannot grow or shrink. If you want to extend an array, you can only create a new array and copy the elements of the original array to the new array.

Memory contiguous:This means that it can be kept in the cache for a longer time and the search speed is faster. It is a very efficient data structure. It also means that it can be numerically (arr[index]) Index the element in the array.

Fixed type:Fixed type means that it limits what data each array element can store and how many bytes of data each element can store.

An array is a fixed-length data type. Its length and the data type of the stored elements are determined when the array is declared and cannot be changed. If you need to store more elements, you must first create a longer array and then copy the data in the original array to the new array.

The memory occupied by the array is allocated continuously. For example, we create an array containing 5 integer elements:

arr1 := [5]int{10,20,30,40,50}
Copy after login

The array is in memory The structure is similar to the following figure:

What does golang array store?

Due to the continuous memory, the CPU can cache the data being used for a longer period of time. And it is very easy to calculate the index when the memory is continuous, which means that all elements in the array can be quickly iterated.

The reason is that the type information of the array can provide the distance that needs to be moved in memory each time an element is accessed. Since each element of the array has the same type and is allocated continuously, it can be moved at a fixed speed. Index any element in an array, very fast!

The above is the detailed content of What does golang array store?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!